File tree Expand file tree Collapse file tree 7 files changed +303
-0
lines changed
Expand file tree Collapse file tree 7 files changed +303
-0
lines changed Original file line number Diff line number Diff line change 1+ /*
2+ * @lc app=leetcode.cn id=144 lang=javascript
3+ *
4+ * [144] 二叉树的前序遍历
5+ */
6+
7+ // @lc code=start
8+ /**
9+ * Definition for a binary tree node.
10+ * function TreeNode(val) {
11+ * this.val = val;
12+ * this.left = this.right = null;
13+ * }
14+ */
15+ /**
16+ * @param {TreeNode } root
17+ * @return {number[] }
18+ */
19+ var preorderTraversal = function ( root ) {
20+ let arr = [ ]
21+ function helper ( root , arr ) {
22+ if ( root !== null ) {
23+ arr . push ( root . val ) ; // 处理本层逻辑
24+ if ( root . left !== null ) {
25+ helper ( root . left , arr ) // 进入下一层
26+ }
27+ if ( root . right !== null ) { // 进入下一层, 这里其实有点分治的意思,左右分别处理
28+ helper ( root . right , arr )
29+ }
30+ }
31+ }
32+ helper ( root , arr )
33+ return arr
34+ } ;
35+ // @lc code=end
36+
Original file line number Diff line number Diff line change 1+
2+ /**
3+ * @param {string } s
4+ * @param {string } t
5+ * @return {boolean }
6+ * 解法一: 暴力解法; 时间复杂度为O(nlogn) + O(nlogn) = O(nlogn)
7+ */
8+ var isAnagram = function ( s , t ) {
9+ if ( s . length !== t . length ) {
10+ return false
11+ }
12+ var s_arr = [ ...s ]
13+ var t_arr = [ ...t ]
14+ s_arr . sort ( )
15+ t_arr . sort ( )
16+ s = JSON . stringify ( s_arr )
17+ t = JSON . stringify ( t_arr )
18+ return s === t
19+ } ;
20+
21+
22+ /**
23+ * @param {string } s
24+ * @param {string } t
25+ * @return {boolean }
26+ * 解法二: 把字符传唤为0-25的数字, 对数字所在数组+-
27+ */
28+ var isAnagram = function ( s , t ) {
29+ if ( s . length != t . length ) {
30+ return false
31+ }
32+ var arr = new Array ( 26 ) . fill ( 0 )
33+
34+ for ( let i = 0 ; i < s . length ; i ++ ) {
35+ arr [ s . charCodeAt ( i ) - 97 ] ++
36+ arr [ t . charCodeAt ( i ) - 97 ] --
37+ }
38+
39+ for ( let item of arr ) {
40+ if ( item !== 0 ) {
41+ return false
42+ }
43+ }
44+ return true
45+ } ;
Original file line number Diff line number Diff line change 1+ /*
2+ * @lc app=leetcode.cn id=429 lang=javascript
3+ *
4+ * [429] 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+ * @param {Node } root
17+ * @return {number[][] }
18+ */
19+ var levelOrder = function ( root ) {
20+ let arr = [ ]
21+ function helper ( root , arr , level ) {
22+ if ( root === null ) {
23+ return
24+ }
25+ if ( arr [ level ] === undefined ) {
26+ arr [ level ] = [ ]
27+ }
28+ arr [ level ] . push ( root . val )
29+ if ( root . children && root . children . length > 0 ) {
30+ for ( let item of root . children ) {
31+ helper ( item , arr , level + 1 )
32+ }
33+ }
34+
35+ }
36+ helper ( root , arr , 0 )
37+ return arr
38+ } ;
39+ // @lc code=end
40+
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+ * @param {Node } root
17+ * @return {number[] }
18+ */
19+ var preorder = function ( root ) {
20+ let arr = [ ] ;
21+ function helper ( root , arr , k ) {
22+ if ( root !== null ) {
23+ arr . push ( root . val ) ;
24+ if ( root . children && root . children . length > 0 ) {
25+ for ( let item of root . children ) {
26+ helper ( item , arr ) ;
27+ }
28+ }
29+ }
30+ }
31+ helper ( root , arr ) ;
32+ return arr ;
33+ } ;
34+ // @lc code=end
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+ * @param {Node } root
17+ * @return {number[] }
18+ */
19+ var postorder = function ( root ) {
20+ let arr = [ ]
21+ function helper ( root , arr ) {
22+ if ( root !== null ) {
23+ if ( root . children && root . children . length > 0 ) {
24+ for ( let item of root . children ) {
25+ helper ( item , arr )
26+ }
27+ }
28+ arr . push ( root . val )
29+ }
30+ }
31+ helper ( root , arr )
32+ return arr
33+ } ;
34+ // @lc code=end
35+
Original file line number Diff line number Diff line change 1+ /*
2+ * @lc app=leetcode.cn id=94 lang=javascript
3+ *
4+ * [94] 二叉树的中序遍历
5+ */
6+
7+ // @lc code=start
8+ /**
9+ * Definition for a binary tree node.
10+ * function TreeNode(val) {
11+ * this.val = val;
12+ * this.left = this.right = null;
13+ * }
14+ */
15+ /**
16+ * @param {TreeNode } root
17+ * @return {number[] }
18+ */
19+ var inorderTraversal = function ( root ) {
20+ let arr = [ ] ;
21+ function helper ( root ) {
22+ if ( root !== null ) {
23+ if ( root . left !== null ) {
24+ helper ( root . left , arr )
25+ }
26+ arr . push ( root . val )
27+ if ( root . right !== null ) {
28+ helper ( root . right , arr )
29+ }
30+ }
31+ }
32+ helper ( root )
33+ return arr
34+ } ;
35+ // @lc code=end
36+
Original file line number Diff line number Diff line change 1+ /*
2+ * @lc app=leetcode.cn id=98 lang=javascript
3+ *
4+ * [98] 验证二叉搜索树
5+ */
6+
7+ // @lc code=start
8+ /**
9+ * Definition for a binary tree node.
10+ * function TreeNode(val) {
11+ * this.val = val;
12+ * this.left = this.right = null;
13+ * }
14+ */
15+ /**
16+ * @param {TreeNode } root
17+ * @return {boolean }
18+ */
19+ var isValidBST = function ( root ) {
20+ function helper ( root , lower , upper ) {
21+ //stop
22+ if ( root === null ) return true
23+
24+ //loginc
25+ let val = root . val
26+ if ( lower !== null && val <= lower ) return false
27+ if ( upper !== null && val >= upper ) return false
28+
29+ //drill down
30+ // 左子节点上界为直接父节点, 下界为往上找,找到第一节点是另一节点的右节点,这个另一个节点就是下界。
31+ /**
32+ * 10
33+ * \
34+ * 15
35+ * /
36+ * 12
37+ * /
38+ * 11
39+ * 11的上界为12, 下界为10,因为15及其之后的节点都要比10大,即(10< ? < X) 找的轨迹就是
40+ *
41+ * \
42+ * /
43+ * /
44+ * /
45+ *
46+ */
47+ if ( ! helper ( root . left , lower , val ) ) return false
48+
49+ if ( ! helper ( root . right , val , upper ) ) return false
50+ // 右子节点上界为直接父节点, 上界为找到的第一个节点是另一个节点的左子节点, 这个另一个节点就是上界
51+ /**
52+ * 15
53+ * /
54+ * 12
55+ * \
56+ * 13
57+ * \
58+ * 14
59+ * 14的下界为13, 上界为15, 因为15的左边节点都要小于它, ( X<?<15 ), 找的轨迹就是
60+ *
61+ *
62+ * /
63+ * \
64+ * \
65+ * \
66+ *
67+ */
68+
69+ return true
70+
71+ }
72+
73+ return helper ( root , null , null )
74+
75+ } ;
76+ // @lc code=end
77+
You can’t perform that action at this time.
0 commit comments