二叉树day6
110. 平衡二叉树
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
//后序递归 满足条件返回高度不满足则返回-1
// class Solution {
// public boolean isBalanced(TreeNode root) {
// return postOrder(root) == -1 ? false : true;
// }
// private int postOrder(TreeNode root) {
// if (root == null) return 0;
// //左右中
// int lh = postOrder(root.left);
// if (lh == -1) return lh;
// int rh = postOrder(root.right);
// if (rh == -1) return rh;
// return Math.abs(lh - rh) <= 1 ? Math.max(lh, rh) + 1 : -1;
// }
// }
// //递归层序遍历每个节点,递归求子树最大深度
// class Solution {
// public boolean isBalanced(TreeNode root) {
// if (root == null) return true;
// if (Math.abs(getDepth(root.left) - getDepth(root.right)) > 1) return false;
// return isBalanced(root.left) && isBalanced(root.right);
// }
// private int getDepth(TreeNode root) {
// if (root == null) return 0;
// return Math.max(getDepth(root.left), getDepth(root.right)) + 1;
// }
// }
//迭代层序遍历每个节点,后序求子树最大深度,最大深度即该结点高度
class Solution {
public boolean isBalanced(TreeNode root) {
Deque queue = new LinkedList<>();
if (root != null) queue.offer(root);
while (!queue.isEmpty()) {
TreeNode cur = queue.poll();
if (Math.abs(getDepth(cur.left) - getDepth(cur.right)) <= 1) {
if (cur.left != null) queue.offer(cur.left);
if (cur.right != null) queue.offer(cur.right);
continue;
}
return false;
}
return true;
}
//递归
// private int getDepth(TreeNode root) {
// if (root == null) return 0;
// return Math.max(getDepth(root.left), getDepth(root.right)) + 1;
// }
//迭代 用空指针标记已遍历未处理的结点
private int getDepth(TreeNode root) {
Deque stack = new LinkedList<>();
if (root != null) stack.push(root);
int depth = 0, res = 0;
while (!stack.isEmpty()) {
TreeNode cur = stack.peek();
if (cur != null) {
//中右左 出栈左中右
depth++;
stack.push(null);
if (cur.right != null) stack.push(cur.right);
if (cur.left != null) stack.push(cur.left);
} else {
//空指针标记退栈
stack.pop();
cur = stack.pop();
depth--;
}
res = res > depth ? res : depth;
//以上过程可以在脑海中构造一个三个结点的满二叉树模拟一遍运行过程自然就懂了
}
return res;
}
}
257. 二叉树的所有路径
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
//递归 List存路径
class Solution {
public List binaryTreePaths(TreeNode root) {
List res = new ArrayList<>();
if (root == null) return res;
List route = new ArrayList<>();
getRoute(root, res, route);
return res;
}
private void getRoute(TreeNode root, List res, List route) {
//中左右
route.add(root.val);
if (root.left == null && root.right == null) {
StringBuffer sb = new StringBuffer();
int size = route.size();
for (int i = 0; i < size - 1; i++) {
sb.append(route.get(i)).append("->");
}
sb.append(route.get(size - 1));
res.add(sb.toString());
}
if (root.left != null) {
getRoute(root.left, res, route);
//回溯
route.remove(route.size() - 1);
}
if (root.right != null) {
getRoute(root.right, res, route);
//回溯
route.remove(route.size() - 1);
}
}
}
//迭代前序
// class Solution {
// public List binaryTreePaths(TreeNode root) {
// List res = new ArrayList<>();
// Deque
404. 左叶子之和
//后序递归
class Solution {
public int sumOfLeftLeaves(TreeNode root) {
return postOrder(root);
}
private int postOrder(TreeNode root) {
if (root == null) return 0;
int sum = 0;
//左右中
if (root.left != null && root.left.left == null && root.left.right == null)
sum += postOrder(root.left) + root.left.val;
else
sum += postOrder(root.left);
if (root.right != null) sum += postOrder(root.right);
return sum;
}
}
迭代法,前中后序都可以,这里我用最容易理解的前序遍历。
//前序迭代
class Solution {
public int sumOfLeftLeaves(TreeNode root) {
Deque stack = new LinkedList<>();
if (root != null) stack.push(root);
int res = 0;
while (!stack.isEmpty()) {
TreeNode cur = stack.pop();
if (cur.left != null && cur.left.left == null && cur.left.right == null)
res += cur.left.val;
if (cur.right != null) stack.push(cur.right);
if (cur.left != null) stack.push(cur.left);
}
return res;
}
}
为啥空间复杂度比迭代还高呢
参考:programmercarl.com