@[TOP](leetcode 树)
前中后序遍历
前序遍历:父节点 -> 左子节点 -> 右子节点
中序遍历:左子节点 -> 父节点 -> 右子节点
后序遍历:左子节点 -> 右子节点 -> 父节点
leetcode144 非递归实现二叉树的前序遍历(Medium)
https://leetcode-cn.com/problems/binary-tree-preorder-traversal/
输入: [1,null,2,3]
1
\
2
/
3
输出: [1,2,3]
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while(!stack.isEmpty()) {
TreeNode node = stack.pop();
if(node == null) continue;
list.add(node.val);
stack.push(node.right);
stack.push(node.left);
}
return list;
}
}
leetcode145 非递归实现二叉树的后序遍历(hard)
前序遍历为 root -> left -> right,后序遍历为 left -> right -> root。可以修改前序遍历成为 root -> right -> left,那么这个顺序就和后序遍历正好相反。
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while(!stack.isEmpty()){
TreeNode node = stack.pop();
if(node == null) continue;
list.add(node.val);
stack.push(node.left);
stack.push(node.right);
}
Collections.reverse(list); //反转List集合中的元素,返回值为void
return list;
}
}
leetcode94 非递归实现二叉树的中序遍历(Medium)
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
if(root == null) return list;
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
while(cur != null || !stack.isEmpty()){
while(cur != null){
stack.push(cur);
cur = cur.left;
}
TreeNode node = stack.pop();
list.add(node.val);
cur = node.right;
}
return list;
}
}
递归
leetcode104 树的高度(Easy)
public int maxDepth(TreeNode root) {
if (root == null) return 0;
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
}
leetcode110 平衡树(Easy)
一棵高度平衡二叉树定义为:
一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1。
class Solution {
private boolean result = true;
public boolean isBalanced(TreeNode root) {
maxDepth(root);
return result;
}
public int maxDepth(TreeNode root){
if(root == null) return 0;
int leftDepth = maxDepth(root.left);
int rightDepth = maxDepth(root.right);
if(Math.abs(leftDepth - rightDepth) > 1) result = false;
return Math.max(leftDepth, rightDepth) + 1;
}
}
leetcode543 两节点的最长路径(Easy)
一棵二叉树的直径长度是任意两个结点路径长度中的最大值。这条路径可能穿过也可能不穿过根结点。
两结点之间的路径长度是以它们之间边的数目表示。
class Solution {
private int max = 0;
public int diameterOfBinaryTree(TreeNode root) {
maxDepth(root);
return max;
}
public int maxDepth(TreeNode root){
if(root == null) return 0;
int leftDepth = maxDepth(root.left);
int rightDepth = maxDepth(root.right);
max = Math.max(max, (leftDepth + rightDepth));
return Math.max(leftDepth,rightDepth) + 1;
}
}
leetcode226 翻转树(Easy)
class Solution {
public TreeNode invertTree(TreeNode root) {
if(root == null) return null;
TreeNode left = root.left; //后面的操作会改变 left 指针,因此先保存下来
root.left = invertTree(root.right);
root.right = invertTree(left);
return root;
}
}
leetcode617 归并两棵树(Easy)
力扣
如果两个节点重叠,那么将他们的值相加作为节点合并后的新值,否则不为 NULL 的节点将直接作为新二叉树的节点。
class Solution {
public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
if(t1 == null && t2 == null) return null;
if(t1 == null) return t2;
if(t2 == null) return t1;
TreeNode root = new TreeNode(t1.val + t2.val);
root.left = mergeTrees(t1.left, t2.left