leetcode 树(java)

这篇博客详细介绍了如何在LeetCode上解决关于二叉树的各种问题,包括非递归实现的前序、中序、后序遍历,递归遍历,树的高度、平衡性,最长路径,翻转、合并、路径和判断等。还涉及到了二叉查找树(BST)的操作,如修剪、查找第k小元素、加值、最近公共祖先等。此外,还讨论了层次遍历和Trie树(前缀树)的实现。
摘要由CSDN通过智能技术生成

@[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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值