File tree 3 files changed +112
-0
lines changed
solution/0500-0599/0513.Find Bottom Left Tree Value
3 files changed +112
-0
lines changed Original file line number Diff line number Diff line change @@ -125,6 +125,45 @@ class Solution {
125
125
}
126
126
```
127
127
128
+ ### ** TypeScript**
129
+
130
+ ``` ts
131
+ /**
132
+ * Definition for a binary tree node.
133
+ * class TreeNode {
134
+ * val: number
135
+ * left: TreeNode | null
136
+ * right: TreeNode | null
137
+ * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
138
+ * this.val = (val===undefined ? 0 : val)
139
+ * this.left = (left===undefined ? null : left)
140
+ * this.right = (right===undefined ? null : right)
141
+ * }
142
+ * }
143
+ */
144
+
145
+ function findBottomLeftValue(root : TreeNode | null ): number {
146
+ let stack: Array <TreeNode > = [root ];
147
+ let ans = root .val ;
148
+ while (stack .length ) {
149
+ let next = [];
150
+ for (let node of stack ) {
151
+ if (node .left ) {
152
+ next .push (node .left );
153
+ }
154
+ if (node .right ) {
155
+ next .push (node .right );
156
+ }
157
+ }
158
+ if (next .length ) {
159
+ ans = next [0 ].val ;
160
+ }
161
+ stack = next ;
162
+ }
163
+ return ans ;
164
+ };
165
+ ```
166
+
128
167
### ** C++**
129
168
130
169
``` cpp
Original file line number Diff line number Diff line change @@ -102,6 +102,45 @@ class Solution {
102
102
}
103
103
```
104
104
105
+ ### ** TypeScript**
106
+
107
+ ``` ts
108
+ /**
109
+ * Definition for a binary tree node.
110
+ * class TreeNode {
111
+ * val: number
112
+ * left: TreeNode | null
113
+ * right: TreeNode | null
114
+ * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
115
+ * this.val = (val===undefined ? 0 : val)
116
+ * this.left = (left===undefined ? null : left)
117
+ * this.right = (right===undefined ? null : right)
118
+ * }
119
+ * }
120
+ */
121
+
122
+ function findBottomLeftValue(root : TreeNode | null ): number {
123
+ let stack: Array <TreeNode > = [root ];
124
+ let ans = root .val ;
125
+ while (stack .length ) {
126
+ let next = [];
127
+ for (let node of stack ) {
128
+ if (node .left ) {
129
+ next .push (node .left );
130
+ }
131
+ if (node .right ) {
132
+ next .push (node .right );
133
+ }
134
+ }
135
+ if (next .length ) {
136
+ ans = next [0 ].val ;
137
+ }
138
+ stack = next ;
139
+ }
140
+ return ans ;
141
+ };
142
+ ```
143
+
105
144
### ** C++**
106
145
107
146
``` cpp
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for a binary tree node.
3
+ * class TreeNode {
4
+ * val: number
5
+ * left: TreeNode | null
6
+ * right: TreeNode | null
7
+ * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8
+ * this.val = (val===undefined ? 0 : val)
9
+ * this.left = (left===undefined ? null : left)
10
+ * this.right = (right===undefined ? null : right)
11
+ * }
12
+ * }
13
+ */
14
+
15
+ function findBottomLeftValue ( root : TreeNode | null ) : number {
16
+ let stack : Array < TreeNode > = [ root ] ;
17
+ let ans = root . val ;
18
+ while ( stack . length ) {
19
+ let next = [ ] ;
20
+ for ( let node of stack ) {
21
+ if ( node . left ) {
22
+ next . push ( node . left ) ;
23
+ }
24
+ if ( node . right ) {
25
+ next . push ( node . right ) ;
26
+ }
27
+ }
28
+ if ( next . length ) {
29
+ ans = next [ 0 ] . val ;
30
+ }
31
+ stack = next ;
32
+ }
33
+ return ans ;
34
+ } ;
You can’t perform that action at this time.
0 commit comments