Skip to content

Commit fa1a2c3

Browse files
committed
Added tasks 103-117
1 parent 1509e95 commit fa1a2c3

File tree

6 files changed

+384
-0
lines changed
  • LeetCodeNet/G0101_0200
    • S0103_binary_tree_zigzag_level_order_traversal
    • S0106_construct_binary_tree_from_inorder_and_postorder_traversal
    • S0108_convert_sorted_array_to_binary_search_tree
    • S0112_path_sum
    • S0117_populating_next_right_pointers_in_each_node_ii

6 files changed

+384
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Net/LeetCode-in-Net?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Net/LeetCode-in-Net?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net/fork)
3+
4+
## 103\. Binary Tree Zigzag Level Order Traversal
5+
6+
Medium
7+
8+
Given the `root` of a binary tree, return _the zigzag level order traversal of its nodes' values_. (i.e., from left to right, then right to left for the next level and alternate between).
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg)
13+
14+
**Input:** root = [3,9,20,null,null,15,7]
15+
16+
**Output:** [[3],[20,9],[15,7]]
17+
18+
**Example 2:**
19+
20+
**Input:** root = [1]
21+
22+
**Output:** [[1]]
23+
24+
**Example 3:**
25+
26+
**Input:** root = []
27+
28+
**Output:** []
29+
30+
**Constraints:**
31+
32+
* The number of nodes in the tree is in the range `[0, 2000]`.
33+
* `-100 <= Node.val <= 100`
34+
35+
## Solution
36+
37+
```csharp
38+
using System.Collections.Generic;
39+
using LeetCodeNet.Com_github_leetcode;
40+
41+
/**
42+
* Definition for a binary tree node.
43+
* public class TreeNode {
44+
* public int val;
45+
* public TreeNode left;
46+
* public TreeNode right;
47+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
48+
* this.val = val;
49+
* this.left = left;
50+
* this.right = right;
51+
* }
52+
* }
53+
*/
54+
public class Solution {
55+
public IList<IList<int>> ZigzagLevelOrder(TreeNode root) {
56+
var queue = new Queue<TreeNode>();
57+
var results = new List<IList<int>>();
58+
if (root == null) {
59+
return results;
60+
}
61+
var level = new List<int>();
62+
queue.Enqueue(root);
63+
queue.Enqueue(null);
64+
var d = false;
65+
while (queue.Count > 0) {
66+
var c = queue.Dequeue();
67+
if (c == null) {
68+
if (d) {
69+
level.Reverse();
70+
}
71+
results.Add(level);
72+
if (queue.Count == 0) {
73+
break;
74+
} else {
75+
queue.Enqueue(null);
76+
level = new List<int>();
77+
d = !d;
78+
}
79+
} else {
80+
level.Add((int)c.val);
81+
if (c.left != null) {
82+
queue.Enqueue(c.left);
83+
}
84+
if (c.right != null) {
85+
queue.Enqueue(c.right);
86+
}
87+
}
88+
}
89+
return results;
90+
}
91+
}
92+
```
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Net/LeetCode-in-Net?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Net/LeetCode-in-Net?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net/fork)
3+
4+
## 106\. Construct Binary Tree from Inorder and Postorder Traversal
5+
6+
Medium
7+
8+
Given two integer arrays `inorder` and `postorder` where `inorder` is the inorder traversal of a binary tree and `postorder` is the postorder traversal of the same tree, construct and return _the binary tree_.
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2021/02/19/tree.jpg)
13+
14+
**Input:** inorder = [9,3,15,20,7], postorder = [9,15,7,20,3]
15+
16+
**Output:** [3,9,20,null,null,15,7]
17+
18+
**Example 2:**
19+
20+
**Input:** inorder = [-1], postorder = [-1]
21+
22+
**Output:** [-1]
23+
24+
**Constraints:**
25+
26+
* `1 <= inorder.length <= 3000`
27+
* `postorder.length == inorder.length`
28+
* `-3000 <= inorder[i], postorder[i] <= 3000`
29+
* `inorder` and `postorder` consist of **unique** values.
30+
* Each value of `postorder` also appears in `inorder`.
31+
* `inorder` is **guaranteed** to be the inorder traversal of the tree.
32+
* `postorder` is **guaranteed** to be the postorder traversal of the tree.
33+
34+
## Solution
35+
36+
```csharp
37+
using LeetCodeNet.Com_github_leetcode;
38+
39+
/**
40+
* Definition for a binary tree node.
41+
* public class TreeNode {
42+
* public int val;
43+
* public TreeNode left;
44+
* public TreeNode right;
45+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
46+
* this.val = val;
47+
* this.left = left;
48+
* this.right = right;
49+
* }
50+
* }
51+
*/
52+
public class Solution {
53+
public TreeNode BuildTree(int[] inorder, int[] postorder) {
54+
int inIndex = inorder.Length - 1;
55+
int postIndex = postorder.Length - 1;
56+
return Helper(inorder, postorder, ref inIndex, ref postIndex, int.MaxValue);
57+
}
58+
59+
private TreeNode Helper(int[] inorder, int[] postorder, ref int inIndex, ref int postIndex, int target) {
60+
if (inIndex < 0 || inorder[inIndex] == target) {
61+
return null;
62+
}
63+
TreeNode root = new TreeNode(postorder[postIndex--]);
64+
root.right = Helper(inorder, postorder, ref inIndex, ref postIndex, (int)root.val);
65+
inIndex--;
66+
root.left = Helper(inorder, postorder, ref inIndex, ref postIndex, target);
67+
return root;
68+
}
69+
}
70+
```
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Net/LeetCode-in-Net?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Net/LeetCode-in-Net?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net/fork)
3+
4+
## 108\. Convert Sorted Array to Binary Search Tree
5+
6+
Easy
7+
8+
Given an integer array `nums` where the elements are sorted in **ascending order**, convert _it to a **height-balanced** binary search tree_.
9+
10+
A **height-balanced** binary tree is a binary tree in which the depth of the two subtrees of every node never differs by more than one.
11+
12+
**Example 1:**
13+
14+
![](https://assets.leetcode.com/uploads/2021/02/18/btree1.jpg)
15+
16+
**Input:** nums = [-10,-3,0,5,9]
17+
18+
**Output:** [0,-3,9,-10,null,5]
19+
20+
**Explanation:** [0,-10,5,null,-3,null,9] is also accepted: ![](https://assets.leetcode.com/uploads/2021/02/18/btree2.jpg)
21+
22+
**Example 2:**
23+
24+
![](https://assets.leetcode.com/uploads/2021/02/18/btree.jpg)
25+
26+
**Input:** nums = [1,3]
27+
28+
**Output:** [3,1]
29+
30+
**Explanation:** [1,3] and [3,1] are both a height-balanced BSTs.
31+
32+
**Constraints:**
33+
34+
* <code>1 <= nums.length <= 10<sup>4</sup></code>
35+
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>
36+
* `nums` is sorted in a **strictly increasing** order.
37+
38+
## Solution
39+
40+
```csharp
41+
using LeetCodeNet.Com_github_leetcode;
42+
43+
/**
44+
* Definition for a binary tree node.
45+
* public class TreeNode {
46+
* public int val;
47+
* public TreeNode left;
48+
* public TreeNode right;
49+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
50+
* this.val = val;
51+
* this.left = left;
52+
* this.right = right;
53+
* }
54+
* }
55+
*/
56+
public class Solution {
57+
public TreeNode SortedArrayToBST(int[] nums) {
58+
return MakeTree(nums, 0, nums.Length - 1);
59+
}
60+
61+
private TreeNode MakeTree(int[] nums, int left, int right) {
62+
if (left > right) {
63+
return null;
64+
}
65+
int mid = (left + right) / 2;
66+
TreeNode midNode = new TreeNode(nums[mid]);
67+
midNode.left = MakeTree(nums, left, mid - 1);
68+
midNode.right = MakeTree(nums, mid + 1, right);
69+
return midNode;
70+
}
71+
}
72+
```
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Net/LeetCode-in-Net?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Net/LeetCode-in-Net?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net/fork)
3+
4+
## 112\. Path Sum
5+
6+
Easy
7+
8+
Given the `root` of a binary tree and an integer `targetSum`, return `true` if the tree has a **root-to-leaf** path such that adding up all the values along the path equals `targetSum`.
9+
10+
A **leaf** is a node with no children.
11+
12+
**Example 1:**
13+
14+
![](https://assets.leetcode.com/uploads/2021/01/18/pathsum1.jpg)
15+
16+
**Input:** root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22
17+
18+
**Output:** true
19+
20+
**Example 2:**
21+
22+
![](https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg)
23+
24+
**Input:** root = [1,2,3], targetSum = 5
25+
26+
**Output:** false
27+
28+
**Example 3:**
29+
30+
**Input:** root = [1,2], targetSum = 0
31+
32+
**Output:** false
33+
34+
**Constraints:**
35+
36+
* The number of nodes in the tree is in the range `[0, 5000]`.
37+
* `-1000 <= Node.val <= 1000`
38+
* `-1000 <= targetSum <= 1000`
39+
40+
## Solution
41+
42+
```csharp
43+
using LeetCodeNet.Com_github_leetcode;
44+
45+
/**
46+
* Definition for a binary tree node.
47+
* public class TreeNode {
48+
* public int val;
49+
* public TreeNode left;
50+
* public TreeNode right;
51+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
52+
* this.val = val;
53+
* this.left = left;
54+
* this.right = right;
55+
* }
56+
* }
57+
*/
58+
public class Solution {
59+
public bool HasPathSum(TreeNode root, int sum) {
60+
if (root == null) {
61+
return false;
62+
}
63+
if (sum == root.val && root.left == null && root.right == null) {
64+
return true;
65+
}
66+
return HasPathSum(root.left, (int)(sum - root.val)) || HasPathSum(root.right, (int)(sum - root.val));
67+
}
68+
}
69+
```
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Net/LeetCode-in-Net?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Net/LeetCode-in-Net?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net/fork)
3+
4+
## 117\. Populating Next Right Pointers in Each Node II
5+
6+
Medium
7+
8+
Given a binary tree
9+
10+
struct Node { int val; Node \*left; Node \*right; Node \*next; }
11+
12+
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to `NULL`.
13+
14+
Initially, all next pointers are set to `NULL`.
15+
16+
**Example 1:**
17+
18+
![](https://assets.leetcode.com/uploads/2019/02/15/117_sample.png)
19+
20+
**Input:** root = [1,2,3,4,5,null,7]
21+
22+
**Output:** [1,#,2,3,#,4,5,7,#]
23+
24+
**Explanation:** Given the above binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
25+
26+
**Example 2:**
27+
28+
**Input:** root = []
29+
30+
**Output:** []
31+
32+
**Constraints:**
33+
34+
* The number of nodes in the tree is in the range `[0, 6000]`.
35+
* `-100 <= Node.val <= 100`
36+
37+
**Follow-up:**
38+
39+
* You may only use constant extra space.
40+
* The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.
41+
42+
## Solution
43+
44+
```csharp
45+
public class Node {
46+
public int val;
47+
public Node left;
48+
public Node right;
49+
public Node next;
50+
51+
public Node() { }
52+
53+
public Node(int _val) {
54+
val = _val;
55+
}
56+
57+
public Node(int _val, Node _left, Node _right, Node _next) {
58+
val = _val;
59+
left = _left;
60+
right = _right;
61+
next = _next;
62+
}
63+
}
64+
```

0 commit comments

Comments
 (0)