当日任务
110.平衡二叉树
257. 二叉树的所有路径
404.左叶子之和
222.完全二叉树的节点个数
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;
* }
* }
*/
class Solution {
public boolean isBalanced(TreeNode root) {
return getHeight(root) == -1 ? false : true;
}
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(leftHeight - rightHeight) > 1) {return -1;}
return Math.max(leftHeight, rightHeight) + 1;
}
}
257. 二叉树的所有路径
题目链接 . - 力扣(LeetCode)
/**
* 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;
* }
* }
*/
class Solution {
public List<String> binaryTreePaths(TreeNode root) {
Stack<Object> stack = new Stack<Object>();
List<String> result = new ArrayList<>();
if (root == null) {return result;}
stack.push(root);
stack.push(root.val + "");
while (!stack.isEmpty()) {
String tmpStr = (String)stack.pop();
TreeNode tmpNode = (TreeNode)stack.pop();
if (tmpNode.left == null && tmpNode.right == null) {
result.add(tmpStr);
}
if (tmpNode.right != null) {
stack.push(tmpNode.right);
stack.push(tmpStr + "->" + tmpNode.right.val);
}
if (tmpNode.left != null) {
stack.push(tmpNode.left);
stack.push(tmpStr + "->" + tmpNode.left.val);
}
}
return result;
}
}
/**
* 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;
* }
* }
*/
class Solution {
public List<String> binaryTreePaths(TreeNode root) {
List<TreeNode> paths = new ArrayList<TreeNode>();
List<String> result = new ArrayList<String>();
if (root == null) {return result;}
inOrdertravel(root, paths, result);
return result;
}
void inOrdertravel(TreeNode root, List<TreeNode> paths, List<String> result){
paths.add(root); //中
if (root.left == null && root.right == null) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < paths.size() - 1; i++) {
sb.append(paths.get(i).val).append("->");
}
sb.append(paths.get(paths.size() - 1).val);
result.add(sb.toString());
return;
}
if (root.right != null) { //右
inOrdertravel(root.right, paths, result);
paths.remove(paths.size() - 1); //回溯
}
if (root.left != null) { //左
inOrdertravel(root.left, paths, result);
paths.remove(paths.size() - 1); //回溯
}
}
}
404.左叶子之和
题目链接 . - 力扣(LeetCode)
/**
* 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;
* }
* }
*/
class Solution {
public int res = 0;
public int sumOfLeftLeaves(TreeNode root) {
if (root == null) return res;
getLeftVal(root, false);
return res;
}
public void getLeftVal(TreeNode root, Boolean ifLeft){
if (root.left == null && root.right == null && ifLeft == true) {
res += root.val;
}
if (root.left != null) {
getLeftVal(root.left, true);
}
if (root.right != null) {
getLeftVal(root.right, false);
}
return;
}
}
/**
* 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;
* }
* }
*/
class Solution {
public int sumOfLeftLeaves(TreeNode root) {
Stack<Object> stack = new Stack<>();
int res = 0;
if (root == null) {return res;}
stack.push(Boolean.FALSE);
stack.push(root);
while (!stack.isEmpty()) {
TreeNode tmpNode = (TreeNode)stack.pop();
boolean ifLeft = (Boolean)stack.pop();
if (ifLeft == Boolean.TRUE && tmpNode.left == null && tmpNode.right == null) {
res += tmpNode.val;
}
if (tmpNode.right != null) {
stack.push(Boolean.FALSE);
stack.push(tmpNode.right);
}
if (tmpNode.left != null) {
stack.push(Boolean.TRUE);
stack.push(tmpNode.left);
}
}
return res;
}
}
222.完全二叉树的节点个数
题目链接 . - 力扣(LeetCode)
/**
* 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;
* }
* }
*/
class Solution {
public int countNodes(TreeNode root) {
Stack<TreeNode> stack = new Stack<TreeNode>();
int len = 0;
if (root == null) {return len;}
stack.push(root);
while (!stack.isEmpty()) {
TreeNode tmp = stack.peek();
if (tmp != null) {
stack.pop();
if (tmp.right != null){stack.push(tmp.right);}
if (tmp.left != null){stack.push(tmp.left);}
stack.push(tmp);
stack.push(null);
} else {
stack.pop();
stack.pop();
len++;
}
}
return len;
}
}