We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
原题链接:https://leetcode-cn.com/problems/binary-tree-level-order-traversal/
解题思路:
/** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } */ function recursion(node, res, index) { // 如果当前节点不存在,则退出递归 if (!node) { return; } // 将节点的值,按照当前层的index存入结果 res[index] ? res[index].push(node.val) : (res[index] = [node.val]); // 计算下一层的index const newIndex = index + 1; // 遍历子节点,并传入下一层的index recursion(node.left, res, newIndex); recursion(node.right, res, newIndex); } /** * @param {TreeNode} root * @return {number[][]} */ var levelOrder = function (root) { let result = []; // 储存结果 // 递归遍历所有节点 recursion(root, result, 0); return result; };
The text was updated successfully, but these errors were encountered:
No branches or pull requests
原题链接:https://leetcode-cn.com/problems/binary-tree-level-order-traversal/
解题思路:
The text was updated successfully, but these errors were encountered: