LeetCode | 102. Binary Tree Level Order Traversal, 226. Invert Binary Tree, 101. Symmetric Tree

102. Binary Tree Level Order Traversal

Link: https://leetcode.com/problems/binary-tree-level-order-traversal/

Description

Given the root of a binary tree, return the level order traversal of its nodes’ values. (i.e., from left to right, level by level).

Approach

Breadth-Frist Search (BFS)

  • Initialize a queue and enqueue the root.
  • While the queue is not empty:
    • Compute the size of the queue.
    • while size > 0:
      • Dequeue the head of the queue and enqueue its children (If not null);
      • size–;

Recursion

  • Initialize a result list, the order of the list is the order of the level, and a recursive functio. The parameters of the functions are the processing node and its depth, the depth of root is 0;
  • Call the function with parameter root and 0.
  • If the root is null and back to the former recursion.
  • Add the node to the result list according to its depth.
  • Make recursive call on node’s children (If not null) with depth++ ;

Solution

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
 //BFS
class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> result = new ArrayList<>();
        if (root == null)
            return result;
        Queue<TreeNode> q = new ArrayDeque<>();
        q.add(root);
        while (!q.isEmpty()) {
            List<Integer> list = new ArrayList<>();
            int size = q.size();
            for (int i = 0; i < size; i++) {
                TreeNode temp = q.remove();
                list.add(temp.val);
                if (temp.left != null)
                    q.add(temp.left);
                if (temp.right != null)
                    q.add(temp.right);
            }
            result.add(list);
        }
        return result;
    }
}

//Recursion
class Solution {
    public List<List<Integer>> result = new ArrayList<>();
    public List<List<Integer>> levelOrder(TreeNode root) {
        levelOrder(root, 0);
        return result;
    }

    private void levelOrder(TreeNode root, int deep) {
        if (root == null)
            return;
        deep++;
        if (deep > result.size()) {
            List<Integer> list = new ArrayList<>();
            result.add(list);
        }
        result.get(deep - 1).add(root.val);
        levelOrder(root.left, deep);
        levelOrder(root.right, deep);
    }
}

226. Invert Binary Tree

Link: https://leetcode.com/problems/invert-binary-tree/

Description

Given the root of a binary tree, invert the tree, and return its root.

Approach

BFS

  • Initialize a queue and enqueue the root.
  • While the queue is not empty:
    • Compute the size of the queue.
    • while size > 0:
      • Dequeue the head of the queue;
      • Swap the left child and right child of the head.
      • Add the left child and right child to the queue (If not null).
      • size–;

Recursion

  • Initialize a recursive function. The parameter of the function is the processing node;
  • Call the function with parameter root.
  • If the root is null and back to the former recursion.
  • Swap the node’s left child and right child.
  • Make recursive calsl on node’s children (If not null);

Solution

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
 //BFS
class Solution {
    public TreeNode invertTree(TreeNode root) {
        invert(root);
        return root;
    }

    private void invert(TreeNode root) {
        if (root == null)
            return;
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
        invert(root.left);
        invert(root.right);
    }
}

//Recursion
class Solution {
    public TreeNode invertTree(TreeNode root) {
        if (root == null)
            return root;
        Queue<TreeNode> queue = new ArrayDeque<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode node = queue.remove();
                TreeNode temp = node.left;
                node.left = node.right;
                node.right = temp;
                if (node.left != null)
                    queue.add(node.left);
                if (node.right != null)
                    queue.add(node.right);
            }
        }
        return root;
    }
}

101. Symmetric Tree

Link:https://leetcode.com/problems/symmetric-tree/

Description

Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).

Approach

BFS

  • Initialize a queue and enqueue the left child and right child root.
  • While the queue is not empty:
    • Dequeue the firt child (left child) and second child (right child) of the queue;
    • If the two child are both equal to null, continue.
    • Else if one of the children is null or the value of the two children is not equal, return false.
    • Add the left child of the left child, right child of the right child, right child of the left child, left child of the right child to the queue in order to check the symmetry.

Recursion

  • Initialize a recursive function. The parameter of the function is two nodes;
  • Call the function with the left child of root and right child of the root.
  • If the two parameters are both equal to null, return true and back to the former recursion.
  • Else if one of the children is null or the value of the two children is not equal, return false.
  • Make two recursive calls with the left tree of left child and the right tree of right child, as well as the right tree of left child and the left tree of right child.

Solution

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
//Recursive
class Solution {
    public boolean isSymmetric(TreeNode root) {
        return check(root.left, root.right);
    }

    private boolean check(TreeNode left, TreeNode right) {
        if (left == null && right == null)
            return true;
        else if ((left == null && right != null) || (left != null && right == null))
            return false;
        else if (left.val != right.val)
            return false;
        else
            return check(left.left, right.right) && check(left.right, right.left);  
    }
}

// BFS
class Solution {
    public boolean isSymmetric(TreeNode root) {
        //ArrayDeque cannot add NULL
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root.left);
        queue.add(root.right);
        while (!queue.isEmpty()) {
            TreeNode left = queue.remove();
            TreeNode right = queue.remove();
            if (left == null && right == null)
                continue;
            else if (left == null || right == null || left.val != right.val)
                return false;
            else {
                queue.add(left.left);
                queue.add(right.right);
                queue.add(left.right);
                queue.add(right.left);
            }
        }
        return true;
    }
}

Remark

ArrayDeque cannot add NULL to the queue, so in this problem I use LinkedList, which can add null to the list.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值