二叉树

先序遍历非递归

//https://leetcode-cn.com/problems/binary-tree-preorder-traversal/
  public List<Integer> preorderTraversal(TreeNode root) {
        if(root==null){
            return new ArrayList<>();
        }

        List<Integer> list=new ArrayList<>();
        Stack<TreeNode> stack=new Stack<>();
        stack.push(root);
        //头左右
        while (!stack.isEmpty()){
            TreeNode treeNode = stack.pop();
            list.add(treeNode.val);
            //先压右,栈是后进后出的,左边遍历比右快
            if(treeNode.right!=null){
               stack.push(treeNode.right);
            }
            if(treeNode.left!=null){
                stack.push(treeNode.left);
            }
        }
        return list;
    }

中序遍历非递归

//https://leetcode-cn.com/problems/binary-tree-inorder-traversal/
 List<Integer> list=new ArrayList<>();
        Stack<TreeNode> stack=new Stack<>();
        while (root!=null||!stack.isEmpty()){
            //一直找左边
            if(root!=null){
                stack.push(root);
                root=root.left;
            }else{
                //左边没有找右边
                TreeNode node=stack.pop();
                list.add(node.val);
                root=node.right;
            }
        }
        return list;

后序遍历非递归

//https://leetcode-cn.com/problems/binary-tree-postorder-traversal/
  public List<Integer> postorderTraversal(TreeNode root) {

        if(root==null){
            return new ArrayList<>();
        }
        ArrayList<Integer> list=new ArrayList<>();
        Stack<TreeNode> stack=new Stack<>();
        //反转栈
        Stack<TreeNode> stack1=new Stack<>();
        stack.push(root);
        //头右左
        while (!stack.isEmpty()){
            TreeNode treeNode=stack.pop();
            stack1.push(treeNode);
            if(treeNode.left!=null){
                stack.push(treeNode.left);
            }
            if(treeNode.right!=null){
                stack.push(treeNode.right);
            }
        }
        System.out.println(stack);
        //左右头
        while (!stack1.isEmpty()){
            list.add(stack1.pop().val);
        }
        return list;

    }

树的路径总和I

//https://leetcode-cn.com/problems/path-sum/submissions/
class Solution {
    boolean flag=false;
    public boolean hasPathSum(TreeNode root, int targetSum) {
        hasPathSum(root,targetSum,0);
        return flag;

    }
    
    public void hasPathSum(TreeNode root,int targetSum,int cur){
        if(root!=null){
            int num=root.val+cur;
            if(root.left==null&&root.right==null&&num==targetSum){
                flag=true;
            }
            hasPathSum(root.left,targetSum,num);
            hasPathSum(root.right,targetSum,num);
        }
    }
}

求和路径

//https://leetcode-cn.com/problems/paths-with-sum-lcci/
class Solution {
    int count=0;
    public int pathSum(TreeNode root, int sum) {
        if(root!=null){
            pathSum(root,sum,0);
            pathSum(root.left,sum);
            pathSum(root.right,sum);
        }
        return this.count;
    }

    public void pathSum(TreeNode root,int sum,int cur){
        if(root!=null){
            int num=cur+root.val;
            System.out.println(num);
            if(num==sum){
                    System.out.println(1);
                    this.count++;
            }
            pathSum(root.left,sum,num);
            pathSum(root.right,sum,num);
        }
    }
}

二叉树的深度

//https://leetcode-cn.com/problems/er-cha-shu-de-shen-du-lcof/
class Solution {
    public int maxDepth(TreeNode root) {

        if(root!=null){
            return Math.max(maxDepth(root.left),maxDepth(root.right))+1;
        }
        return 0;

    }
}

判断树是否平衡

class Solution {
    boolean flag=true;
    public boolean isBalanced(TreeNode root) {
        //注意剪枝条件
        if(root!=null&&flag==true){
            int left=height(root.left);
            int right=height(root.right);
            if(Math.abs(left-right)>1){
                flag=false;
            }
            isBalanced(root.left);
            isBalanced(root.right);
        }
        return flag;
    }
    public int height(TreeNode node){
        if(node!=null){
            int left=height(node.left);
            int right=height(node.right);
            return Math.max(left,right)+1;
        }
        return 0;
    }
}

平衡二叉树第k大结点

//https://leetcode-cn.com/problems/er-cha-sou-suo-shu-de-di-kda-jie-dian-lcof/
class Solution {
    List<Integer> list=new ArrayList();
    public int kthLargest(TreeNode root, int k) {
        order(root);
        return list.get(list.size()-k);
    }
    public void order(TreeNode root){
        if(root!=null){
            order(root.left);
            list.add(root.val);
            order(root.right);
        }
    }
}

二叉树的剪枝

//https://leetcode-cn.com/problems/binary-tree-pruning/comments/
class Solution {
    public TreeNode pruneTree(TreeNode root) {
           if(root!=null){
            TreeNode left=pruneTree(root.left);
            TreeNode  right=pruneTree(root.right);
            root.left=left;
            root.right=right;
            if((left==null)&&(right==null)&&root.val==0){
                return null;
            }
            return root;
        }
        return null;
        
    }
}

二叉树搜索树的公共祖先

//https://leetcode-cn.com/problems/er-cha-sou-suo-shu-de-zui-jin-gong-gong-zu-xian-lcof/
 //利用二叉树搜索树的有序性
class Solution {
    TreeNode node=null;
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        dfs(root,p,q);
        return node;


    }

    //注意p,q大小不知道谁大谁小都需要讨论好
    void dfs(TreeNode root, TreeNode p, TreeNode q){
        if(root!=null&&node==null){
            if(root.val>=p.val&&root.val<=q.val&&node==null){
                node=root;
            }
            else if(root.val>=q.val&&root.val<=p.val&&node==null){
                node=root;
            }
            dfs(root.left,p,q);
            dfs(root.right,p,q);
        }
    }
}

二叉树的祖先问题

//https://leetcode-cn.com/problems/er-cha-shu-de-zui-jin-gong-gong-zu-xian-lcof/submissions/
class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root!=null){
            TreeNode left=lowestCommonAncestor(root.left, p, q);
            TreeNode right=lowestCommonAncestor(root.right,p,q);
            if(root==p||root==q){
                return root;
            }
            if(left!=null&&right!=null){
                return root;
            }
            if(right!=null&&left==null){
                return right;
            }
            if(left!=null&&right==null){
                return left;
            }
            if(left==null&&right==null){
                return null;
            }
        }
        return null;   
    }
}

具有所有最深节点的最小子树

class Solution {
    public TreeNode subtreeWithAllDeepest(TreeNode root) {
       List<TreeNode> treeNode=getTreeNode(root);
       System.out.println(treeNode);
       return dfs(root,treeNode);
    }

    //获取深度最深的结点的集合
    private List<TreeNode> getTreeNode(TreeNode root) {
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        HashMap<TreeNode,Integer> map=new HashMap<>();
        List<TreeNode> list=new ArrayList<>();
        int max=Integer.MIN_VALUE;
        map.put(root,1);
        while (!queue.isEmpty()){
            int size = queue.size();
            for (int i=0;i<size;i++){
                TreeNode node = queue.poll();       
                int height = map.get(node); 
                max=Math.max(height,max);
                if(node.left!=null){
                    map.put(node.left,height+1);
                    queue.offer(node.left);
                }
                if(node.right!=null){
                    map.put(node.right,height+1);
                    queue.offer(node.right);
                }
            }
        }
        for(TreeNode treeNode:map.keySet()){
            if(map.get(treeNode)==max){
                list.add(treeNode);
            }
        }
        return list;
    }

    //获取最深结点的共同祖先的模板,后序遍历
    public TreeNode dfs(TreeNode root,List<TreeNode> list){
        if(root!=null){
            TreeNode left=dfs(root.left,list);
            TreeNode right=dfs(root.right,list);
            if(list.contains(root)){
                return root;
            }
            if(left==null&&right==null){
                return null;
            }
            if(left!=null&&right==null){
                return left;
            }
            if(left==null&&right!=null){
                return right;
            }
            if(left!=null&&right!=null){
                return root;
            }

        }
        return null;
    }
}

二叉树最大宽度(算空值)

//https://leetcode-cn.com/problems/maximum-width-of-binary-tree/submissions/
class Solution {
    public int widthOfBinaryTree(TreeNode root) {
       if(root==null){
            return 0;
        }
        Deque<TreeNode> queue=new LinkedList<>();
        queue.add(root);
        HashMap<TreeNode,Integer> map=new HashMap<>();
        map.put(root,1);
        int max=Integer.MIN_VALUE;
        while (!queue.isEmpty()){
            int size=queue.size();
            int left=map.get(queue.getFirst());
            int right=map.get(queue.getLast());
            max=Math.max(max,right-left+1);
            for (int i=0;i<size;i++){
                TreeNode treeNode = queue.pollFirst();
                Integer pos = map.get(treeNode);
                //利用满二叉树的思想
                if(treeNode.left!=null){
                    queue.offerLast(treeNode.left);
                    map.put(treeNode.left,2*pos);
                }
                if(treeNode.right!=null){
                    queue.offerLast(treeNode.right);
                    map.put(treeNode.right,2*pos+1);
                }
            }
        }
        return max;    
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值