Skip to main content

Overview of Binary Tree

⛳ Overview

Binary Tree is an important data structure, and it's widely used in BFS, DFS, DP, Back tracking, Divide and conquer algorithms.

📃 Analysis

tip

3 Tree Traversals

  • Inorder: Left, Root, Right
  • Preorder: Root, Left, Right
  • Postorder: Left, Right,Root

Examples:

Binary tree

Inorder: 4 2 5 6 3 7 1
Preorder: 1 2 4 5 3 6 7
PostOrder: 4 5 2 6 7 3 1

✏ Code Editor

1. Print the level of each node

Live Editor
Result

Total test cases: 1


Input: "{
  "val": 1,
  "left": {
    "val": 2,
    "left": {
      "val": 4,
      "left": null,
      "right": null
    },
    "right": {
      "val": 5,
      "left": null,
      "right": null
    }
  },
  "right": {
    "val": 3,
    "left": {
      "val": 6,
      "left": null,
      "right": null
    },
    "right": {
      "val": 7,
      "left": null,
      "right": null
    }
  }
}"
Output: " {
  "1": {
    "node": {
      "val": 1,
      "left": {
        "val": 2,
        "left": {
          "val": 4,
          "left": null,
          "right": null
        },
        "right": {
          "val": 5,
          "left": null,
          "right": null
        }
      },
      "right": {
        "val": 3,
        "left": {
          "val": 6,
          "left": null,
          "right": null
        },
        "right": {
          "val": 7,
          "left": null,
          "right": null
        }
      }
    },
    "depth": 1
  },
  "2": {
    "node": {
      "val": 3,
      "left": {
        "val": 6,
        "left": null,
        "right": null
      },
      "right": {
        "val": 7,
        "left": null,
        "right": null
      }
    },
    "depth": 2
  },
  "3": {
    "node": {
      "val": 7,
      "left": null,
      "right": null
    },
    "depth": 3
  }
}"

2. Print the number of leafs of each node

Live Editor
Result

Total test cases: 1


Input: "{
  "val": 1,
  "left": {
    "val": 2,
    "left": {
      "val": 4,
      "left": null,
      "right": null
    },
    "right": {
      "val": 5,
      "left": null,
      "right": null
    }
  },
  "right": {
    "val": 3,
    "left": {
      "val": 6,
      "left": null,
      "right": null
    },
    "right": {
      "val": 7,
      "left": null,
      "right": null
    }
  }
}"
Output: "7 {
  "1": {
    "node": {
      "val": 1,
      "left": {
        "val": 2,
        "left": {
          "val": 4,
          "left": null,
          "right": null
        },
        "right": {
          "val": 5,
          "left": null,
          "right": null
        }
      },
      "right": {
        "val": 3,
        "left": {
          "val": 6,
          "left": null,
          "right": null
        },
        "right": {
          "val": 7,
          "left": null,
          "right": null
        }
      }
    },
    "count": 7
  },
  "2": {
    "node": {
      "val": 3,
      "left": {
        "val": 6,
        "left": null,
        "right": null
      },
      "right": {
        "val": 7,
        "left": null,
        "right": null
      }
    },
    "count": 3
  },
  "3": {
    "node": {
      "val": 7,
      "left": null,
      "right": null
    },
    "count": 1
  }
}"