Day 66 of 100 Days of code

1.Binary Tree Zigzag Level Order Traversal

class Solution {
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        Queue<TreeNode> q=new LinkedList<>();
        List<List<Integer>> list=new ArrayList<>();
        if(root==null){
            return list;
        }
        q.offer(root);
        boolean flag=true;
        while(!q.isEmpty()){
            int level=q.size();
            ArrayList<Integer> sub=new ArrayList<>();
            for(int i=0;i<level;i++){
                if(q.peek().left!=null){q.offer(q.peek().left);}
                if(q.peek().right!=null){q.offer(q.peek().right);}
                if(flag==true){sub.add(q.poll().val);}
                else{sub.add(0,q.poll().val);}
            }
            flag=!flag;
            list.add(sub);
        }
        return list;
    }
    
}        

TC: O(N)

SC: O(N)

2.Sum Root to Leaf Numbers

class Solution {
    public static int total;
    public int sumNumbers(TreeNode root) {
        total=0;
        helper(root,0);
        return total;
    }
    public static void helper(TreeNode root,int sum){
        if(root==null){return;}
        sum=10*sum+root.val;
        if(root.left==null &&root.right==null){
            total+=sum;
        }
        helper(root.left,sum);
        helper(root.right,sum);
    }
}        

TC: O(N)

SC: O(N)

3.Diameter of Binary Tree

class Solution {
    public int diameterOfBinaryTree(TreeNode root) {
        int []diameter=new int[1];
        height(root,diameter);
        return diameter[0];
    }
    public static int height(TreeNode root,int[] diameter){
        if(root==null){
            return 0;
        }
        int lh= height(root.left,diameter);
        int rh= height(root.right,diameter);
        diameter[0]=Math.max(diameter[0],lh+rh);
        return 1+Math.max(lh,rh);
    }
}        

TC: O(N)

SC: O(N)


要查看或添加评论,请登录

Saidhanya Sree的更多文章

  • Day 85 of 100 Days of code

    Day 85 of 100 Days of code

    1.Trapping Rain Water Time complexity: O(N) Space complexity: O(1)

    1 条评论
  • Day 84 of 100 Days of code

    Day 84 of 100 Days of code

    1.Word Search Time : O(n*m*4^L) space: O(n*m+L) 2.

  • Day 83 of 100 Days of coding

    Day 83 of 100 Days of coding

    1.M-Coloring Problem Time complexity: O(N^M) space: O(N) for color array

    1 条评论
  • Day 82 of 100 Days of code

    Day 82 of 100 Days of code

    1.N-Queens Time complexity: O(N! * N).

    1 条评论
  • Day 81 of 100 Days of code

    Day 81 of 100 Days of code

    1.Combination Sum Time complexity: O(2^t*k) t=target and k=avg length Space complexity:O(k*x) k=avg length x=no of…

  • Day 80 of 100 Days of code

    Day 80 of 100 Days of code

    1.Subsets code: Time complexity: O(2^n) Space complexity:O(1)

    1 条评论
  • Day 79 of 100 Days of code

    Day 79 of 100 Days of code

    1.Find Peak Element Time complexity : O(NlogN) Space complexity: O(1) 2.

    1 条评论
  • Day 78 of 100 Days of coding

    Day 78 of 100 Days of coding

    1.Search in Rotated Sorted Array II Time complexity : O(logn) Space complexity :O(1) 2.

  • Day 77 of 100 Days of coding

    Day 77 of 100 Days of coding

    1.Maximum Product Subarray 2.

    1 条评论
  • Day 76 of 100 Days of Coding

    Day 76 of 100 Days of Coding

    1.Next Permutation Time complexity : O(3N) space complexity: O(1)

社区洞察

其他会员也浏览了