公司外贸网站怎么做,17zwd一起做业网站,手机做网站自己做,WordPress全站广告输入两个整数序列#xff0c;第一个序列表示栈的压入顺序#xff0c;请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如#xff0c;序列 {1,2,3,4,5} 是某栈的压栈序列#xff0c;序列 {4,5,3,2,1} 是该压栈序列对应的一个弹出序列#xff0c;但…输入两个整数序列第一个序列表示栈的压入顺序请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列 {1,2,3,4,5} 是某栈的压栈序列序列 {4,5,3,2,1} 是该压栈序列对应的一个弹出序列但 {4,3,5,1,2} 就不可能是该压栈序列的弹出序列。 示例 1
输入pushed [1,2,3,4,5], popped [4,5,3,2,1] 输出true 解释我们可以按以下顺序执行 push(1), push(2), push(3), push(4), pop() - 4, push(5), pop() - 5, pop() - 3, pop() - 2, pop() - 1 示例 2
输入pushed [1,2,3,4,5], popped [4,3,5,1,2] 输出false 解释1 不能在 2 之前弹出。
提示
0 pushed.length popped.length 1000 0 pushed[i], popped[i] 1000 pushed 是 popped 的排列。
思路模拟每压入一个就尝试弹出到不能再弹。到最后栈空就可以。
class Solution {public boolean validateStackSequences(int[] pushed, int[] popped) {StackInteger stack new StackInteger();int j 0;for(int i 0;i pushed.length;i){stack.push(pushed[i]);while(!stack.empty() stack.peek() popped[j]){stack.pop();j;}}return stack.empty();}
} 从上到下打印出二叉树的每个节点同一层的节点按照从左到右的顺序打印。 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回
[3,9,20,15,7]
提示
节点总数 1000
思路层序遍历出队列时用另一个list记录最后转为数组即可。
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode(int x) { val x; }* }*/
class Solution {public int[] levelOrder(TreeNode root) {if (root null) return new int[0];QueueTreeNode queue new LinkedList();ListInteger res new ArrayList();queue.add(root);while(!queue.isEmpty()) {TreeNode node queue.poll();res.add(node.val);if(node.left ! null) queue.add(node.left);if(node.right ! null) queue.add(node.right);}int[] _result new int[res.size()];for (int i 0; i res.size(); i) {_result[i] res.get(i);}return _result;}
} 从上到下按层打印二叉树同一层的节点按从左到右的顺序打印每一层打印到一行。 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回其层次遍历结果
[ [3], [9,20], [15,7] ]
提示
节点总数 1000
思路遍历时一次弹完一层所有节点队列内当时的所有节点并用一个list保存。
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode(int x) { val x; }* }*/
class Solution {public ListListInteger levelOrder(TreeNode root) {QueueTreeNode queue new LinkedList();ListListInteger res new ArrayList();if(root ! null) queue.add(root);while(!queue.isEmpty()) {ListInteger tmp new ArrayList();for(int i queue.size(); i 0; i--) {TreeNode node queue.poll();tmp.add(node.val);if(node.left ! null) queue.add(node.left);if(node.right ! null) queue.add(node.right);}res.add(tmp);}return res;}
} 请实现一个函数按照之字形顺序打印二叉树即第一行按照从左到右的顺序打印第二层按照从右到左的顺序打印第三行再按照从左到右的顺序打印其他行以此类推。 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回其层次遍历结果
[ [3], [20,9], [15,7] ]
提示
节点总数 1000
思路和上题一样只不过加一句话翻转部分temp。
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode(int x) { val x; }* }*/
class Solution {public ListListInteger levelOrder(TreeNode root) {QueueTreeNode queue new LinkedList();ListListInteger res new ArrayList();if(root ! null) queue.add(root);while(!queue.isEmpty()) {ListInteger tmp new ArrayList();for(int i queue.size(); i 0; i--) {TreeNode node queue.poll();tmp.add(node.val);if(node.left ! null) queue.add(node.left);if(node.right ! null) queue.add(node.right);}if(res.size() % 2 1) Collections.reverse(tmp);res.add(tmp);}return res;}
}