Skip to content
This repository was archived by the owner on Aug 14, 2024. It is now read-only.

Latest commit

 

History

History
58 lines (46 loc) · 1.06 KB

102.md

File metadata and controls

58 lines (46 loc) · 1.06 KB

102. Binary Tree Level Order Traversal

Description

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).

For example: Given binary tree [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

return its level order traversal as:

[
  [3],
  [9,20],
  [15,7]
]

Thinking Process

  1. Use two queues, one for current level, one for the next level

Code

class Solution {
    public boolean isSubtree(TreeNode s, TreeNode t) {
        if(s == null){
            return false;
        }else if(s.val == t.val && isSameTree(s, t)){
            return true;
        }
        else{
            return isSubtree(s.left, t) || isSubtree(s.right, t);
        }
    }
    
    boolean isSameTree(TreeNode s, TreeNode t){
        if(s == null && t == null){
            return true;
        }
        if(s != null && t != null && s.val == t.val){
            return isSameTree(s.left, t.left) && isSameTree(s.right, t.right);
        }
        return false;
    }
}