File tree Expand file tree Collapse file tree 5 files changed +178
-0
lines changed
Expand file tree Collapse file tree 5 files changed +178
-0
lines changed Original file line number Diff line number Diff line change 1+ /*
2+ * @lc app=leetcode.cn id=572 lang=javascript
3+ *
4+ * [572] 另一棵树的子树
5+ */
6+
7+ // @lc code=start
8+ /**
9+ * Definition for a binary tree node.
10+ * function TreeNode(val, left, right) {
11+ * this.val = (val===undefined ? 0 : val)
12+ * this.left = (left===undefined ? null : left)
13+ * this.right = (right===undefined ? null : right)
14+ * }
15+ */
16+ /**
17+ * @param {TreeNode } root
18+ * @param {TreeNode } subRoot
19+ * @return {boolean }
20+ */
21+
22+ var same = function ( root , sub ) {
23+
24+ if ( root === null ) {
25+ if ( sub === null )
26+ return true
27+ return false
28+ }
29+
30+ if ( sub === null ) {
31+ return false
32+ }
33+
34+ if ( root . val !== sub . val ) {
35+ return false
36+ }
37+
38+ return same ( root . left , sub . left ) && same ( root . right , sub . right )
39+ }
40+
41+ var isSubtree = function ( root , subRoot ) {
42+ if ( same ( root , subRoot ) ) {
43+ return true
44+ }
45+ if ( root . left !== null && isSubtree ( root . left , subRoot ) ) {
46+ return true
47+ }
48+ if ( root . right !== null && isSubtree ( root . right , subRoot ) ) {
49+ return true
50+ }
51+ return false
52+ } ;
53+ // @lc code=end
54+
Original file line number Diff line number Diff line change 1+ /*
2+ * @lc app=leetcode.cn id=575 lang=javascript
3+ *
4+ * [575] 分糖果
5+ */
6+
7+ // @lc code=start
8+ /**
9+ * @param {number[] } candyType
10+ * @return {number }
11+ */
12+ var distributeCandies = function ( candyType ) {
13+ let s = new Set ( )
14+ for ( let i = 0 ; i < candyType . length ; i ++ ) {
15+ s . add ( candyType [ i ] )
16+ }
17+ let j = parseInt ( candyType . length / 2 )
18+ return s . size > j ? j : s . size
19+ } ;
20+ // @lc code=end
21+
Original file line number Diff line number Diff line change 1+ /*
2+ * @lc app=leetcode.cn id=589 lang=javascript
3+ *
4+ * [589] N 叉树的前序遍历
5+ */
6+
7+ // @lc code=start
8+ /**
9+ * // Definition for a Node.
10+ * function Node(val, children) {
11+ * this.val = val;
12+ * this.children = children;
13+ * };
14+ */
15+
16+ /**
17+ * @param {Node|null } root
18+ * @return {number[] }
19+ */
20+ var preorder = function ( root ) {
21+ if ( root === null ) {
22+ return [ ]
23+ }
24+ let a = [ ]
25+ a . push ( root )
26+ let rs = [ ]
27+ while ( a . length > 0 ) {
28+ let r = a . pop ( )
29+
30+ rs . push ( r . val )
31+
32+ for ( let i = r . children . length - 1 ; i > - 1 ; i -- ) {
33+ a . push ( r . children [ i ] )
34+ }
35+
36+ }
37+ return rs
38+ } ;
39+ // @lc code=end
40+
Original file line number Diff line number Diff line change 1+ /*
2+ * @lc app=leetcode.cn id=590 lang=javascript
3+ *
4+ * [590] N 叉树的后序遍历
5+ */
6+
7+ // @lc code=start
8+ /**
9+ * // Definition for a Node.
10+ * function Node(val,children) {
11+ * this.val = val;
12+ * this.children = children;
13+ * };
14+ */
15+
16+ /**
17+ * @param {Node|null } root
18+ * @return {number[] }
19+ */
20+ var postorder = function ( root ) {
21+ if ( root === null ) return [ ]
22+ let rs = [ ]
23+ let a = [ ]
24+ a . push ( root )
25+ while ( a . length > 0 ) {
26+ let n = a . pop ( )
27+ rs . unshift ( n . val )
28+ for ( let r of n . children ) {
29+ a . push ( r )
30+ }
31+ }
32+ return rs
33+ } ;
34+ // @lc code=end
35+
Original file line number Diff line number Diff line change 1+ /*
2+ * @lc app=leetcode.cn id=915 lang=javascript
3+ *
4+ * [915] 分割数组
5+ */
6+
7+ // @lc code=start
8+ /**
9+ * @param {number[] } nums
10+ * @return {number }
11+ */
12+ var partitionDisjoint = function ( nums ) {
13+ r = 0
14+ cur_max = nums [ 0 ]
15+ left_max = nums [ 0 ]
16+
17+ for ( let i = 1 ; i < nums . length ; i ++ ) {
18+ cur_max = Math . max ( nums [ i ] , cur_max )
19+ if ( left_max > nums [ i ] ) {
20+ r = i
21+ left_max = cur_max
22+ }
23+ }
24+
25+ return r + 1
26+ } ;
27+ // @lc code=end
28+
You can’t perform that action at this time.
0 commit comments