mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 11:34:46 +08:00
添加二叉树的递归遍历javascript版本
This commit is contained in:
@ -226,7 +226,7 @@ func PreorderTraversal(root *TreeNode) (res []int) {
|
||||
var traversal func(node *TreeNode)
|
||||
traversal = func(node *TreeNode) {
|
||||
if node == nil {
|
||||
return
|
||||
return
|
||||
}
|
||||
res = append(res,node.Val)
|
||||
traversal(node.Left)
|
||||
@ -272,6 +272,41 @@ func PostorderTraversal(root *TreeNode) (res []int) {
|
||||
}
|
||||
```
|
||||
|
||||
javaScript:
|
||||
|
||||
```js
|
||||
|
||||
前序遍历:
|
||||
|
||||
var preorderTraversal = function(root, res = []) {
|
||||
if (!root) return res;
|
||||
res.push(root.val);
|
||||
preorderTraversal(root.left, res)
|
||||
preorderTraversal(root.right, res)
|
||||
return res;
|
||||
};
|
||||
|
||||
中序遍历:
|
||||
|
||||
var inorderTraversal = function(root, res = []) {
|
||||
if (!root) return res;
|
||||
inorderTraversal(root.left, res);
|
||||
res.push(root.val);
|
||||
inorderTraversal(root.right, res);
|
||||
return res;
|
||||
};
|
||||
|
||||
后序遍历:
|
||||
|
||||
var postorderTraversal = function(root, res = []) {
|
||||
if (!root) return res;
|
||||
postorderTraversal(root.left, res);
|
||||
postorderTraversal(root.right, res);
|
||||
res.push(root.val);
|
||||
return res;
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user