Title: Enter the root node of a binary tree to find the depth of the number. From the root node to the leaf node, the nodes (including roots, leaf nodes) Form a path of the tree, the length of the longest path is the depth of the tree.
For example, a two-fork tree has a depth of 4, because its longest path from the root node to the leaf node contains 4 nodes (starting at root node 1, passing 2 and node 5, eventually reaching leaf node 7)
We can understand the depth of the tree from another angle. If a tree has only one node, its depth is 1, if the root node is only Zuozi and there is no right subtree, then the depth of the tree should be the depth of its left subtree +1. Similarly, if the root node has only a right subtree and no left subtree, the depth of the tree should be its right subtree +1. If there are Zuozi and right subtrees, The depth of the outline is the larger value plus 1 of the depth of the left and right sub-trees.
Using this idea, we can implement the code with a recursive return:
Common binary tree Depth public int treedepth (Binarytreenode root) {if (root = null) return 0;int Nleft = treedepth (root.leftnode); int Nright = Treedepth (Root.rightnode); return (Nleft > Nright)? (nleft+1):(nright+1);}
If the company has a high level of programming ability, the interviewer may append a question that is more difficult than the previous one, for example, after the candidate finishes the above questions, the interviewer asks:
Topic Two: Enter a binary tree root node, determine whether the tree is balanced two fork tree. If the depth of the left and right subtree of any node in a binary tree is not more than 1, then it is a balanced binary tree.
The solution of repeated traversal of nodes is simple but not enough to impress the interviewer
With the experience of finding the depth of the binary tree to solve this problem, it is easy to think of a way of thinking: when traversing each node of the tree, the call function treedepth the depth of its left and right subtree. If the depth of the left and right subtree of each node is not more than 1, by definition it is a balanced two-fork tree. This idea implements the following code:
Determine if it is a balanced binary tree public boolean isbalanced (Binarytreenode root) {if (root ==null) return true;int left = treedepth ( Root.leftnode); int right = Treedepth (root.rightnode); int diff = left-right;if (diff > 1 | | diff <-1) return false;re Turn isbalanced (root.leftnode) && isbalanced (Root.rightnode);}
The code above is concise, but we also notice that the time efficiency is not high because a node is iterated over multiple times. For example, in the function isbalanced input in the two-fork tree, we will first determine whether the root node is not balanced. At this point, we need to traverse the node 4,5,7 when we treedepth the root node of the left subtree. Next, you will still traverse the node 4,5,7 if the subtree that is the Node 2 is the tree of the root. There is no doubt that repeating traversal of the same node can affect performance. Next we look for algorithms that don't need to be iterated over.
The solution that each node only traverses once, is the algorithm that the interviewer likes
If we traverse each node of the binary tree in a sequential traversal, we have traversed its left and right subtrees before traversing a node. As long as we log the depth of each node as we traverse it (the depth of a certain point equals the length of its path to the leaf node), we can walk one side and judge whether each node is a balanced binary tree. Here is the implementation code for this idea:
The high-efficiency judgment is a balanced binary tree public boolean isBalanced2 (Binarytreenode root) {int depth = 0;return isBalanced2 (root,depth);} public boolean isBalanced2 (binarytreenode root,int depth) {if (root = null) {depth = 0;return true;} int left = 0,right = 0;if (IsBalanced2 (root.leftnode,left) && isBalanced2 (root.rightnode,right)) {int diff = Left-right;if (diff <= 1 && diff >=-1) {depth = 1+ (left > Right?left:right); return true;}} return false;}
In the above code, we traverse the entire binary tree in the same way as the sequential traversal. After traversing the left and right sub-tree nodes of a node, we can judge it from time to time according to the depth of its subtree, and get the depth of the current node. When we finally traverse to the root node of the tree, it is also judged that the whole binary tree is not a balanced two-pronged tree.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Sword Point offer Face Question (Java Edition): Two depth of the fork tree