104. Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
求从根节点到最远叶子结点路径经过的结点数,分别用了DFS 和 backtracking(bottom up)来求解,思路如下:
1. DFS -----stack
自己定义一个newTreeNode 类,其中包括height属性,root的height为1,那么每往下移动一个结点,它的height就是在前面一个height的基础上+1
主要运用stack来实现,先将root(非null)放入stack中,进入循环。
当stack不为空时,(也就是当前结点有子节点时)
将stack中最上面的元素pop出 赋为 当前结点 cur
再判断cur是否有左右结点,若有则依次push进栈;若无则更新当前最大的depth(因为没有子节点说明到头了。。)
代码实现如下:
public class Solution {
public int maxDepth(TreeNode root) {
if(root == null){
return 0;
}
Stack<myTreeNode> stack = new Stack<>();
stack.push(new myTreeNode(root, 1));
Int depth = 0;
while(! stack.isEmpty()){
myTreeNode cur = stack.pop();
//Operation at cur node
if(cur._node.left == null && cur._node.right == null){
depth = Math.max(depth, cur._height);
}
if(cur._node.left != null){
stack.push(new myTreeNode(cur._node.left, cur._height + 1));
}
if(cur._node.right != null){
stack.push(new myTreeNode(cur._node.right, cur._height + 1));
}
}
}
return depth;
}
private class myTreeNode{
TreeNode _node;
int _height;
public myTreeNode(TreeNode node, int height){
_node = node;
_height = height;
}
}
}
2. backracking---------递归 recursion
从root开始往下找,找到最后一个结点(即左右结点都为null,此时记null结点的height为0),往上找,每一次+1
比较从左右子节点到达当前结点返回的depth的值,哪个大选哪个
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public int maxDepth(TreeNode root) { return root == null ? 0 : Math.max(maxDepth(root.left),maxDepth(root.right))+1; } }
3. BFS-------queue
稍后补
111. Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
同上题类似,但是是找出最小的路径经过的节点数
这一题开始仿照DFS写了如下代码,但是运行速度很慢。。。。附上代码:
这里改变的只有depth的初始值,因为当最后的结点为null的时候返回0,没有比0更小的了,且接下来的depth也只会增加,所以要把初始值设置到最大(如果初始值还为0 的话,当有左右其中一个子节点为null时结果不对),这样就不会出现结果比正确答案少1 的情况。/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public int minDepth(TreeNode root) { if(root == null) return 0; Stack<myTreeNode> stack = new Stack<myTreeNode>(); stack.push(new myTreeNode(1,root)); int depth = Integer.MAX_VALUE; while(!stack.isEmpty()){ myTreeNode cur = stack.pop(); if(cur._node.left == null && cur._node.right == null){ depth = Math.min(cur.height, depth); } if(cur._node.left != null){ stack.push(new myTreeNode(cur.height+1,cur._node.left)); } if(cur._node.right != null){ stack.push(new myTreeNode(cur.height+1,cur._node.right)); } } return depth; } private class myTreeNode{ int height; TreeNode _node; public myTreeNode(int height, TreeNode _node){ this.height = height; this._node = _node; } } }
快一些的解决方案是用backtracking,附上代码:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public int minDepth(TreeNode root) { if(root == null) return 0; if(root.left == null && root.right == null) return 1; int depth = Integer.MAX_VALUE; if(root.left != null){ depth = Math.min(depth,minDepth(root.left)); } if(root.right != null){ depth = Math.min(depth,minDepth(root.right)); } return depth+1; } }
最后depth+1是因为就算到了第二层的子节点,要加上当前子节点的root。所以返回需要+1