16th Day| 222.完全二叉树的节点个数,110.平衡二叉树,257.二叉树的所有路径,404.左叶子之和
LeetCode 222 完全二叉树的节点个数
题目链接:222.完全二叉树的节点个数
//迭代法
class Solution {public int countNodes(TreeNode root) {if(root == null) return 0;Queue<TreeNode> queue = new LinkedList<>();int count = 0;queue.add(root);while(! queue.isEmpty()){int size = queue.size();count+=size;while(size-- > 0){TreeNode tempNode = queue.poll();if(tempNode.left != null) queue.add(tempNode.left);if(tempNode.right != null) queue.add(tempNode.right);}}return count; }
}
//普通二叉树后序遍历
class Solution {public int countNodes(TreeNode root) {if(root == null) return 0;int leftNodes = countNodes(root.left);int rightNodes = countNodes(root.right);return leftNodes+rightNodes+1;}
}
//利用完全二叉树的特性
class Solution {public int countNodes(TreeNode root) {if(root == null) return 0;TreeNode left = root.left;TreeNode right = root.right;int leftDepth = 0, rightDepth = 0;while(left != null){left = left.left;leftDepth++;}while(right !=null){right = right.right;rightDepth++;}if(leftDepth == rightDepth){return (2 << leftDepth)-1;}return countNodes(root.left)+countNodes(root.right)+1;}
}
LeetCode 110 平衡二叉树
题目链接:110.平衡二叉树
/**后序遍历 */
class Solution {public boolean isBalanced(TreeNode root) {int result = getHeight(root);return result != -1;}public int getHeight(TreeNode root){if(root == null) return 0;int leftHeight = getHeight(root.left);if(leftHeight == -1) return -1;int rightHeight = getHeight(root.right);if(rightHeight == -1) return -1;if(Math.abs(rightHeight - leftHeight) > 1) return -1;else return Math.max(rightHeight, leftHeight)+1;}
}
LeetCode 257 二叉树的所有路径
题目链接:257.二叉树的所有路径
/**完整版 */
class Solution {public List<String> binaryTreePaths(TreeNode root) {List<String> result = new ArrayList<>();if(root == null) return result;List<Integer> path = new ArrayList<>();searchPaths(root, path, result);return result;}public void searchPaths(TreeNode root, List<Integer> path, List<String> result){path.add(root.val);if(root.left == null && root.right == null){//终止逻辑StringBuilder sb = new StringBuilder();for(int i = 0; i < path.size()-1; i++){sb.append(path.get(i)).append("->");}sb.append(path.get(path.size()-1));result.add(sb.toString());return;}if(root.left != null){searchPaths(root.left, path, result);path.remove(path.size()-1);//回溯}if(root.right != null){searchPaths(root.right, path, result);path.remove(path.size()-1);} }
}/**简洁版 */
class Solution {List<String> result = new ArrayList<>();public List<String> binaryTreePaths(TreeNode root) {searchPaths(root, "");return result;}public void searchPaths(TreeNode root, String s){if(root == null) return;if(root.left == null && root.right == null){result.add(new StringBuilder(s).append(root.val).toString());}String tmp = new StringBuilder(s).append(root.val).append("->").toString();searchPaths(root.left, tmp);searchPaths(root.right, tmp);}
}
LeetCode 404 左叶子之和
题目链接:404.左叶子之和
/**后序遍历 */
class Solution {public int sumOfLeftLeaves(TreeNode root) {if(root == null) return 0;if(root.left == null && root.right == null) return 0;int leftLeaves = sumOfLeftLeaves(root.left);if(root.left != null && root.left.left ==null && root.left.right == null){leftLeaves = root.left.val;}int rightLeaves = sumOfLeftLeaves(root.right);return leftLeaves+rightLeaves;}
}
/** 层序遍历 迭代法*/
class Solution {public int sumOfLeftLeaves(TreeNode root) {if(root == null) return 0;Queue<TreeNode> queue = new LinkedList<>();queue.add(root);int result = 0;while(! queue.isEmpty()){int size = queue.size();while(size-- > 0){TreeNode tmp = queue.poll();if(tmp.left != null) queue.add(tmp.left);if(tmp.right != null) queue.add(tmp.right);if(tmp.left != null && tmp.left.left == null && tmp.left.right == null){result+=tmp.left.val;}}}return result;}
}
/** 前序遍历 栈模拟递归 迭代法*/
class Solution {public int sumOfLeftLeaves(TreeNode root) {if(root == null) return 0;Stack<TreeNode> stack = new Stack<>();stack.push(root);int result = 0;while(! stack.empty()){TreeNode tmp = stack.pop();if(tmp.right != null) stack.push(tmp.right);if(tmp.left != null) stack.push(tmp.left);if(tmp.left != null && tmp.left.left == null && tmp.left.right == null){result+=tmp.left.val;}}return result;}
}