leetcode-104-二叉樹的最大深度(maximum depth of binary tree)-
阿新 • • 發佈:2018-12-10
題目及用例
package pid104; /*二叉樹的最大深度 給定一個二叉樹,找出其最大深度。 二叉樹的深度為根節點到最遠葉子節點的最長路徑上的節點數。 說明: 葉子節點是指沒有子節點的節點。 示例: 給定二叉樹 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回它的最大深度 3 。 }*/ public class main { public static void main(String[] args) { Object[] x=new Object[]{3,9,20,null,null,15,7}; BinaryTree tree=new BinaryTree(x); tree.printTree(tree.root); test(tree.root); } private static void test(TreeNode ito) { Solution solution = new Solution(); int rtn; long begin = System.currentTimeMillis(); rtn = solution.maxDepth(ito);//執行程式 long end = System.currentTimeMillis(); System.out.println("rtn=" ); System.out.print(rtn); System.out.println(); System.out.println("耗時:" + (end - begin) + "ms"); System.out.println("-------------------"); } }
樹與節點的類
package pid104;
public class TreeNode {
int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int x) {
val = x;
left=null;
right=null;
}
}
package pid104; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Queue; public class BinaryTree { public TreeNode root; public List<TreeNode> nodes=new ArrayList<TreeNode>(); public BinaryTree(int x){ root=new TreeNode(x); nodes.add(root); } public BinaryTree(Object[] x){ for(int i=0;i<x.length;i++){ if(x[i]!=null){ nodes.add(new TreeNode((int)x[i])); } else{ nodes.add(null); } } root=nodes.get(0); for(int i=0;i<x.length/2;i++){ TreeNode now=nodes.get(i); if(now!=null){ now.left=nodes.get(2*i+1); now.right=nodes.get(2*i+2); } } } public void preOrder(TreeNode root){ if(root==null){ return; } System.out.print(root.val+" "); preOrder(root.left); preOrder(root.right); } public void inOrder(TreeNode root){ if(root==null){ return; } inOrder(root.left); System.out.print(root.val+" "); inOrder(root.right); } public void postOrder(TreeNode root){ if(root==null){ return; } postOrder(root.left); postOrder(root.right); System.out.print(root.val+" "); } public void printTree(TreeNode root){ if(root == null) return; Queue<TreeNode> queue = new LinkedList<TreeNode>(); int current;//當前層 還未列印的結點個數 int next;//下一層結點個數 queue.offer(root); current = 1; next = 0; while(!queue.isEmpty()){ TreeNode currentNode = queue.poll(); if (currentNode!=null) { System.out.print(currentNode.val+" "); current--; } else{ System.out.print("null "); current--; queue.offer(null); next++; queue.offer(null); next++; if(current ==0){ System.out.println(); current = next; next = 0; int temp=0; for (TreeNode treeNode : queue) { if(treeNode==null){ temp++; } } if(temp==current){ System.out.println("end"); break; } } continue; } if(currentNode.left != null){ queue.offer(currentNode.left); next++; } else{ queue.offer(null); next++; } if(currentNode.right != null){ queue.offer(currentNode.right); next++; } else{ queue.offer(null); next++; } if(current ==0){ System.out.println(); current = next; next = 0; int temp=0; for (TreeNode treeNode : queue) { if(treeNode==null){ temp++; } } if(temp==current){ System.out.println("end"); break; } } } } }
解法1(成功,2ms,較慢)
建立兩個list,now和next now的初始值為root length初始為0 每次迴圈 將,now的left與right加入next 然後讓now=next,並且length++ 直到now為null為止
package pid104; import java.util.LinkedList; import java.util.List; /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { public int maxDepth(TreeNode root) { List<TreeNode> now=new LinkedList<TreeNode>(); List<TreeNode> next=new LinkedList<TreeNode>(); if(root==null){ return 0; } int length=0; now.add(root); while(!now.isEmpty()){ for (TreeNode treeNode : now) { if(treeNode.left!=null){ next.add(treeNode.left); } if(treeNode.right!=null){ next.add(treeNode.right); } } now=next; next=new LinkedList<TreeNode>(); length++; } return length; } }
解法2(別人的) 精妙的遞迴演算法, 長度=當前長度(1,因為自己有)+左右的max長度
遞迴求解,遞迴公式 n就是treenode引數 f(n) = 0; n=null, f(n) = 1+ max(f(n左), f(n右))
public class Solution {
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
} else if (root.left == null && root.right == null) {
return 1;
} else {
int left = maxDepth(root.left);
int right = maxDepth(root.right);
return 1 + (left > right ? left : right);
}
}
}