[Sword refers to Offer learning] [interview question 39: depth of Binary Tree], sword refers to offer
Question 1: Enter the root node of a binary tree to find the depth of the tree. A tree path is formed from the root node to the leaf node. The longest path length is the depth of the tree.Binary Tree node Definition
private static class BinaryTreeNode { int val; BinaryTreeNode left; BinaryTreeNode right; public BinaryTreeNode() { } public BinaryTreeNode(int val) { this.val = val; }}
Solutions
If a tree has only one node, its depth is. If the root node has only the left subtree but not the right subtree, the depth of the tree should be the depth of the Left subtree plus 1. Similarly, if the root node only has the right subtree but not the left subtree, the depth of the tree should be the depth of the right subtree plus 1. if there are both right and left subtree, the depth of the tree is the depth of the left and right subtree plus 1. for example, in the binary tree in Figure 6.1, a tree with a root node of 1 has two left and right Subtrees. The root nodes of the left and right Subtrees are nodes 2 and 3 respectively. The depth of the Left subtree with the root node 2 is 3, and the depth of the right subtree with the root node 3 is 2. Therefore, the depth of the tree with the root node 1 is 4.
This idea can be easily implemented using recursive methods. You only need to slightly modify the traversal code.
Code Implementation
public static int treeDepth(BinaryTreeNode root) { if (root == null) { return 0; } int left = treeDepth(root.left); int right = treeDepth(root.right); return left > right ? (left + 1) : (right + 1);}
Question 2: Enter the root node of a binary tree to determine whether the tree is balanced. If the depth difference between left and right Subtrees of any node in a binary tree does not exceed 1, it is a balanced binary tree.
Solutions
Solution 1: solution to traverse nodes multiple times
When traversing each node of the tree, call the treeDepth function to obtain the depth of its left and right subtree. If the depth difference between left and right Subtrees of each node does not exceed 1, it is a balanced binary tree by definition.
public static boolean isBalanced(BinaryTreeNode root) { if (root == null) { return true; } int left = treeDepth(root.left); int right = treeDepth(root.right); int diff = left - right; if (diff > 1 || diff < -1) { return false; } return isBalanced(root.left) && isBalanced(root.right);}
Solution 2: the solution for Traversing each node only once
We use the post-sequential Traversal method to traverse every node of a binary tree. We have traversed its left and right Subtrees before traversing to a node. As long as you record the depth of each node when traversing it (the depth of a node is equal to the length of its path to the leaf node ), we can traverse and judge whether each node is balanced.
/*** Determine whether it is a balanced binary tree. solution 2 ** @ param root * @ return */public static boolean isBalanced2 (BinaryTreeNode root) {int [] depth = new int [1]; return isBalancedHelper (root, depth);} public static boolean isBalancedHelper (BinaryTreeNode root, int [] depth) {if (root = null) {depth [0] = 0; return true;} int [] left = new int [1]; int [] right = new int [1]; if (isBalancedHelper (root. left, left) & isbalancedel Per (root. right, right) {int diff = left [0]-right [0]; if (diff> =-1 & diff <= 1) {depth [0] = 1 + (left [0]> right [0]? Left [0]: right [0]); return true ;}} return false ;}
Complete code
Public class Test39 {private static class BinaryTreeNode {int val; BinaryTreeNode left; BinaryTreeNode right; public BinaryTreeNode () {} public BinaryTreeNode (int val) {this. val = val ;}} public static int treeDepth (BinaryTreeNode root) {if (root = null) {return 0;} int left = treeDepth (root. left); int right = treeDepth (root. right); return left> right? (Left + 1): (right + 1);}/*** determine whether it is a balanced binary tree, solution 1 ** @ param root * @ return */public static boolean isBalanced (BinaryTreeNode root) {if (root = null) {return true ;} int left = treeDepth (root. left); int right = treeDepth (root. right); int diff = left-right; if (diff> 1 | diff <-1) {return false;} return isBalanced (root. left) & isBalanced (root. right);}/*** determines whether a balanced binary tree exists. solution 2 ** @ param Root * @ return */public static boolean isBalanced2 (BinaryTreeNode root) {int [] depth = new int [1]; return isBalancedHelper (root, depth );} public static boolean isBalancedHelper (BinaryTreeNode root, int [] depth) {if (root = null) {depth [0] = 0; return true ;} int [] left = new int [1]; int [] right = new int [1]; if (isBalancedHelper (root. left, left) & isBalancedHelper (root. right, right) {int Diff = left [0]-right [0]; if (diff> =-1 & diff <= 1) {depth [0] = 1 + (left [0]> right [0]? Left [0]: right [0]); return true;} return false;} public static void main (String [] args) {test1 (); test2 (); test3 (); test4 ();} // full Binary Tree // 1 ////// 2 3 //////\///4 5 6 7 private static void test1 () {BinaryTreeNode n1 = new BinaryTreeNode (1); BinaryTreeNode n2 = new BinaryTreeNode (1); BinaryTreeNode n3 = new BinaryTreeNode (1); BinaryTreeNode n4 = new BinaryTreeNode (1 ); required n5 = new BinaryTreeNode (1); BinaryTreeNode n6 = new BinaryTreeNode (1); BinaryTreeNode n7 = new BinaryTreeNode (1); n1.left = n2; n1.right = n3; n2.left = n4; n2.right = n5; n3.left = n6; n3.right = n7; System. out. println (isBalanced (n1); System. out. println (isBalanced2 (n1); System. out. println ("----------------");} // not a Complete Binary Tree, however, the balance Binary Tree // 1 ////2 3 ///\// 4 5 6 //// 7 private static void test2 () {BinaryTreeNode n1 = new BinaryTreeNode (1); BinaryTreeNode n2 = new BinaryTreeNode (1); BinaryTreeNode n3 = new BinaryTreeNode (1); BinaryTreeNode n4 = new BinaryTreeNode (1 ); required n5 = new BinaryTreeNode (1); BinaryTreeNode n6 = new BinaryTreeNode (1); BinaryTreeNode n7 = new BinaryTreeNode (1); n1.left = n2; n1.right = n3; n2.left = n4; n2.right = n5; n5.left = n7; n3.right = n6; System. out. println (isBalanced (n1); System. out. println (isBalanced2 (n1); System. out. println ("----------------");} // not a balanced binary tree // 1 // 2 3 // 4 5 // 7 private static void test3 () {BinaryTreeNode n1 = new BinaryTreeNode (1); BinaryTreeNode n2 = new BinaryTreeNode (1); BinaryTreeNode n3 = new BinaryTreeNode (1); BinaryTreeNode n4 = new BinaryTreeNode (1 ); required n5 = new BinaryTreeNode (1); BinaryTreeNode n6 = new BinaryTreeNode (1); BinaryTreeNode n7 = new BinaryTreeNode (1); n1.left = n2; n1.right = n3; n2.left = n4; n2.right = n5; n5.left = n7; System. out. println (isBalanced (n1); System. out. println (isBalanced2 (n1); System. out. println ("----------------");} // 1 // 2 // 3 // 4 // 5 private static void test4 () {BinaryTreeNode n1 = new BinaryTreeNode (1); BinaryTreeNode n2 = new BinaryTreeNode (1); BinaryTreeNode n3 = new BinaryTreeNode (1); BinaryTreeNode n4 = new BinaryTreeNode (1 ); required n5 = new BinaryTreeNode (1); BinaryTreeNode n6 = new BinaryTreeNode (1); BinaryTreeNode n7 = new BinaryTreeNode (1); n1.left = n2; n2.left = n3; n3.left = n4; n4.left = n5; System. out. println (isBalanced (n1); System. out. println (isBalanced2 (n1); System. out. println ("----------------");} // 1 ////2 ////3 /////4 ////// 5 private static void test5 () {BinaryTreeNode n1 = new BinaryTreeNode (1); BinaryTreeNode n2 = new BinaryTreeNode (1); BinaryTreeNode n3 = new BinaryTreeNode (1); BinaryTreeNode n4 = new BinaryTreeNode (1 ); required n5 = new BinaryTreeNode (1); BinaryTreeNode n6 = new BinaryTreeNode (1); BinaryTreeNode n7 = new BinaryTreeNode (1); n1.right = n2; n2.right = n3; n3.right = n4; n4.right = n5; System. out. println (isBalanced (n1); System. out. println (isBalanced2 (n1); System. out. println ("----------------");}}
Running result
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.