Skip to content

Latest commit

 

History

History
157 lines (112 loc) · 3.24 KB

199.Binary-Tree-Right-Side-View.md

File metadata and controls

157 lines (112 loc) · 3.24 KB

199. Binary Tree Right Side View

Problem


Given the root of 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 1:

https://assets.leetcode.com/uploads/2021/02/14/tree.jpg

Input: root = [1,2,3,null,5,null,4]
Output: [1,3,4]

Example 2:

Input: root = [1,null,3]
Output: [1,3]

Example 3:

Input: root = []
Output: []

Constraints:

  • The number of nodes in the tree is in the range [0, 100].
  • -100 <= Node.val <= 100

Note


Code


  • PHP

    Runtime 3ms Beats 88.14% of users with PHP

    Memory 19.75MB Beats 84.75% of users with PHP

    /*
     * Definition for a binary tree node.
     * class TreeNode {
     *     public $val = null;
     *     public $left = null;
     *     public $right = null;
     *     function __construct($val = 0, $left = null, $right = null) {
     *         $this->val = $val;
     *         $this->left = $left;
     *         $this->right = $right;
     *     }
     * }
     */
    class Solution {
    
        /**
         * @param TreeNode $root
         * @return Integer[]
         */
        function rightSideView($root) {
            $result = [];
            $queue = [$root];
    
            while (!empty($queue)) {
                $tmpQueue = [];
                for ($i = 0; $i < count($queue); $i++) {
                    $node = $queue[$i];
                    if ($i == 0) {
                        $result[] = $node->val;
                    }
    
                    if (!is_null($node->right)) {
                        $tmpQueue[] = $node->right;
                    }
    
                    if (!is_null($node->left)) {
                        $tmpQueue[] = $node->left;
                    }
                }
    
                $queue = $tmpQueue;
            }
    
            return $result;
        }
    }
  • GoLang

    Runtime 0ms Beats 100.00% of users with Go

    Memory 2.52MB Beats **25.46%**of users with Go

    /**
     * Definition for a binary tree node.
     * type TreeNode struct {
     *     Val int
     *     Left *TreeNode
     *     Right *TreeNode
     * }
     */
    func rightSideView(root *TreeNode) []int {
        if root == nil {
            return []int{}
        }
    
        result := []int{}
        queue := []*TreeNode{root}
        
        for len(queue) != 0 {
            newQueue := []*TreeNode{}
            for i := 0; i < len(queue); i++ {
                node := queue[i]
    
                if i == 0 {
                    result = append(result, node.Val)
                }
    
                if node.Right != nil {
                    newQueue = append(newQueue, node.Right)
                }
    
                if node.Left != nil {
                    newQueue = append(newQueue, node.Left)
                }
            }
    
            queue = newQueue
        }
    
        return result
    }

Reference