LeetCode 107
Posted wangtinglou
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 107相关的知识,希望对你有一定的参考价值。
import java.util.*; /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public List<List<Integer>> levelOrderBottom(TreeNode root) { List<List<Integer>> wrapList = new LinkedList<List<Integer>>(); levelMaker(wrapList, root, 0); return wrapList; } public void levelMaker(List<List<Integer>> list, TreeNode root, int level) { if (root == null) return; if (level >= list.size()) { list.add(0, new LinkedList<Integer>()); } levelMaker(list, root.left, level + 1); levelMaker(list, root.right, level + 1); list.get(list.size() - level - 1).add(root.val); } }
以上是关于LeetCode 107的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode_107. Binary Tree Level Order Traversal II
[LeetCode]题解(python):107 Binary Tree Level Order Traversal II