LeetCode Playground & Binary Tree Traversal All In One
Posted xgqfrms
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode Playground & Binary Tree Traversal All In One相关的知识,希望对你有一定的参考价值。
LeetCode Playground & Binary Tree Traversal All In One
非正常的二叉搜索树/排序二叉树生成
Binary Tree Traversal
二叉树遍历
LeetCode Playground & Binary Tree Traversal All In One
非正常的二叉搜索树
/排序二叉树
生成
// binary-tree-generator.ts 不好使
// 手动生成 ✅ 非正常的二叉搜索树/排序二叉树
const node = new TreeNode(1);
node.right = new TreeNode(2);
node.right.left = new TreeNode(3);
console.log(`node =`, node);
https://www.cnblogs.com/xgqfrms/p/17052456.html#5141281
Binary Tree Traversal / 二叉树遍历
// 手动生成 ✅ 非正常的二叉搜索树/排序二叉树
const node = new TreeNode(1);
node.right = new TreeNode(2);
node.right.left = new TreeNode(3);
console.log(`node =`, node);
function preorderTraversal(root: TreeNode | null): number[]
if(!root) return [];
// DFS 深度优先搜索
function dfs(head: TreeNode | null, result: number[])
if(!head)
return;
// 先序遍历
result.push(head.val);
dfs(head.left, result)
dfs(head.right, result);
let result: number[] = [];
dfs(root, result);
return result;
;
const preorder = preorderTraversal(node);
console.log(`preorder tree =`, preorder);
function inorderTraversal(root: TreeNode | null): number[]
if(!root) return [];
// DFS 深度优先搜索
function dfs(head: TreeNode | null, result: number[])
if(!head)
return;
dfs(head.left, result)
// 中序遍历
result.push(head.val);
dfs(head.right, result);
let result: number[] = [];
dfs(root, result);
return result;
;
const tree = inorderTraversal(node);
console.log(`inorder tree =`, tree);
function postorderTraversal(root: TreeNode | null): number[]
if(!root) return [];
// DFS 深度优先搜索
function dfs(head: TreeNode | null, result: number[])
if(!head)
return;
dfs(head.left, result)
dfs(head.right, result);
// 后序遍历
result.push(head.val);
let result: number[] = [];
dfs(root, result);
return result;
;
const postorder = postorderTraversal(node);
console.log(`post tree =`, postorder);
https://leetcode.com/playground/7wyqA5aJ
(以上是关于LeetCode Playground & Binary Tree Traversal All In One的主要内容,如果未能解决你的问题,请参考以下文章
Hacker‘s Playground(SSTF) 2021 Web
Hacker‘s Playground(SSTF) 2021 Web
为啥这个 Swift Playground 打印和循环两次?
以上是关于LeetCode Playground & Binary Tree Traversal All In One的主要内容,如果未能解决你的问题,请参考以下文章
Hacker‘s Playground(SSTF) 2021 Web
Hacker‘s Playground(SSTF) 2021 Web
为啥这个 Swift Playground 打印和循环两次?