8.8 LeetCode 199 Binary Tree Right Side View

Question

link

Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.

For example:
Given the following binary tree,

   1            <---
 /   2     3         <---
 \       5     4       <---

You should return [1, 3, 4].

Show Tags

Tree Depth-first Search Breadth-first Search

Solution

My own solution is BFS and take the last element of each level into a list, but we need two lists so we can make sure clear one level before clear next level. The solution from NineChart uses the feature that the num of seen nodes equals the level, so check right first, if not null put in list, then check left if num < level indicates right is null, put left into list or right is not null, do nothing.

My solution I (No code reuse):

public List<Integer> rightSideView(TreeNode root) {
        LinkedList<Integer> res = new LinkedList<Integer>();
        if(root == null)
            return res;
        LinkedList<TreeNode> list1 = new LinkedList<TreeNode>();
        LinkedList<TreeNode> list2 = new LinkedList<TreeNode>();
        list1.add(root);
        int cnt = 1;
        while(!list1.isEmpty() || !list2.isEmpty()) {
            while(!list1.isEmpty()) {
                TreeNode temp = list1.remove();
                if(list1.isEmpty())
                    res.add(temp.val); // list is null indicate this is the rightest node
                if(temp.left != null) {
                    list2.add(temp.left);
                }
                if(temp.right != null) {
                    list2.add(temp.right);
                }
            }
            while(!list2.isEmpty()) {
                TreeNode temp = list2.remove();
                if(list2.isEmpty())
                    res.add(temp.val); // list is null indicate this is the rightest node
                if(temp.left != null) {
                    list1.add(temp.left);
                }
                if(temp.right != null) {
                    list1.add(temp.right);
                }
            }
        }
        return res;
    }

My solution II (improved by reuse the code of iteration):

public List<Integer> rightSideView(TreeNode root) {
        LinkedList<Integer> res = new LinkedList<Integer>();
        if(root == null)
            return res;
        LinkedList<TreeNode> list1 = new LinkedList<TreeNode>();
        LinkedList<TreeNode> list2 = new LinkedList<TreeNode>();
        list1.add(root);while(!list1.isEmpty() || !list2.isEmpty()) {
            bfs(list1, list2, res); // remove nodes from list1 and put its children into list2
            bfs(list2, list1, res); // remove nodes from list2 and put its children into list1
        }
        return res;
    }
    private void bfs(LinkedList<TreeNode> list1, LinkedList<TreeNode> list2, LinkedList<Integer> res) {
        while(!list1.isEmpty()) {
            TreeNode temp = list1.remove();
            if(list1.isEmpty())
                res.add(temp.val); // list is null indicate this is the rightest node
            if(temp.left != null)
                list2.add(temp.left);
            if(temp.right != null)
                list2.add(temp.right);
        }
    }

Solution of NineCharts (More concise use more time but less space):

public class Solution {
    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> result = new ArrayList<Integer>();
        traverse(result, root, 1);
        return result;
    }

    private void traverse(List<Integer> result, TreeNode node, int level) {
        if (node == null) return;
        if (level > result.size()) {
            result.add(node.val);
        }
        traverse(result, node.right, level + 1);
        traverse(result, node.left, level + 1);
    }
}
时间: 2024-10-26 18:17:02

8.8 LeetCode 199 Binary Tree Right Side View的相关文章

LeetCode 199. Binary Tree Right Side View

1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 vector<int> rightSideView

[LeetCode] 199. Binary Tree Right Side View Java

题目: Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. For example:Given the following binary tree, 1 <--- / 2 3 <--- \ 5 4 <--- You should return [1, 3,

leetcode 199. Binary Tree Right Side View 求所能看到的叶子节点 ---------- java

Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. For example:Given the following binary tree, 1 <--- / 2 3 <--- \ 5 4 <--- You should return [1, 3, 4].

Java for LeetCode 199 Binary Tree Right Side View

Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. For example: Given the following binary tree, 1 <--- / 2 3 <--- \ 5 4 <--- You should return [1, 3, 4].

(二叉树 bfs) leetcode 199. Binary Tree Right Side View

Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. Example: Input: [1,2,3,null,5,null,4] Output: [1, 3, 4] Explanation: 1 <--- / 2 3 <--- \ 5 4 <---------

【LeetCode】199. Binary Tree Right Side View

Binary Tree Right Side View Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. For example:Given the following binary tree, 1 <--- / 2 3 <--- \ 5 4 <--- Y

&lt;LeetCode OJ&gt; 199. Binary Tree Right Side View

Total Accepted: 40438 Total Submissions: 117654 Difficulty: Medium Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. For example: Given the following binary

199. Binary Tree Right Side View

题目: Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. For example:Given the following binary tree, 1 <--- / 2 3 <--- \ 5 4 <--- You should return [1, 3,

[email&#160;protected] [199] Binary Tree Right Side View (DFS/BFS)

https://leetcode.com/problems/binary-tree-right-side-view/ Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. For example: Given the following binary tree, 1