力扣--LCR 149.彩灯装饰记录I
题目
代码
/**
-
Definition for a binary tree node.
-
public class TreeNode {
-
int val;
-
TreeNode left;
-
TreeNode right;
-
TreeNode() {}
-
TreeNode(int val) { this.val = val; }
-
TreeNode(int val, TreeNode left, TreeNode right) {
-
this.val = val;
-
this.left = left;
-
this.right = right;
-
}
-
}
*/
class Solution {
public int[] decorateRecord(TreeNode root) {
if(root == null){
return new int[0];
}Queue<TreeNode> queue = new LinkedList<>(); List<Integer> res = new ArrayList<>(); queue.add(root); while(!queue.isEmpty()){ TreeNode t = queue.poll(); res.add(t.val); if(t.left != null) queue.add(t.left); if(t.right != null) queue.add(t.right); } int[] arr = new int[res.size()]; for(int i = 0; i < res.size(); i++){ arr[i] = res.get(i); } return arr;
}
}
时间复杂度:O(n)
额外空间复杂度:容器里最对存放 1/2 的节点,故为 O(n)
原文地址:https://blog.csdn.net/weixin_52297290/article/details/144145455
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!