Posts

Showing posts with the label pre order

1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree

Image
https://leetcode.com/problems/find-a-corresponding-node-of-a-binary-tree-in-a-clone-of-that-tree/ Given two binary trees  original  and  cloned  and given a reference to a node  target  in the original tree. The  cloned  tree is a  copy of  the  original  tree. Return  a reference to the same node  in the  cloned  tree. Note  that you are  not allowed  to change any of the two trees or the  target  node and the answer  must be  a reference to a node in the  cloned  tree. Follow up:  Solve the problem if repeated values on the tree are allowed.   Example 1: Input: tree = [7,4,3,null,null,6,19], target = 3 Output: 3 Explanation: In all examples the original and cloned trees are shown. The target node is a green node from the original tree. The answer is the yellow node from the cloned tree. Example 2: Input: tree = [7], target = 7 Output: 7 Example 3...

606. Construct String from Binary Tree

https://leetcode.com/problems/construct-string-from-binary-tree/ You need to construct a string consists of parenthesis and integers from a binary tree with the preorder traversing way. The null node needs to be represented by empty parenthesis pair "()". And you need to omit all the empty parenthesis pairs that don't affect the one-to-one mapping relationship between the string and the original binary tree. Example 1: Input: Binary tree: [1,2,3,4] 1 / \ 2 3 / 4 Output: "1(2(4))(3)" Explanation: Originallay it needs to be "1(2(4)())(3()())", but you need to omit all the unnecessary empty parenthesis pairs. And it will be "1(2(4))(3)". Example 2: Input: Binary tree: [1,2,3,null,4] 1 / \ 2 3 \ 4 Output: "1(2()(4))(3)" Explanation: Almost the same as the first example, except we can't omit the first parenthesis pair to break the one-to-one mappin...

1028. Recover a Tree From Preorder Traversal

Image
https://leetcode.com/problems/recover-a-tree-from-preorder-traversal/ We run a preorder depth first search on the  root  of a binary tree. At each node in this traversal, we output  D  dashes (where  D  is the  depth  of this node), then we output the value of this node.   (If the depth of a node is  D , the depth of its immediate child is  D+1 .  The depth of the root node is  0 .) If a node has only one child, that child is guaranteed to be the left child. Given the output  S  of this traversal, recover the tree and return its  root . Example 1: Input: "1-2--3--4-5--6--7" Output: [1,2,5,3,4,6,7] Example 2: Input: "1-2--3---4-5--6---7" Output: [1,2,5,3,null,6,null,4,null,7] Example 3: Input: "1-401--349---90--88" Output: [1,401,null,349,88,90] Note: The number of nodes in the original tree is between  1  and  1000 . Each node will h...