Leetcode刷题--Week4

二叉树前序遍历
题目

Given a binary tree, return the preorder traversal of its nodes’ values.

For example:
Given binary tree{1,#,2,3},
return[1,2,3].

Note: Recursive solution is trivial, could you do it iteratively?

解题思路
递归方法

递归方法比较简单,把左右子树当成一个完整二叉树递归遍历即可。

代码:

public ArrayList<Integer> preorderTraversal(TreeNode root) {
		ArrayList<Integer> list = new ArrayList<>();
		traversal(list,root);
		return list;
	}

	public void traversal(ArrayList<Integer> list, TreeNode root){
		if (root == null){
			return;
		}
		list.add(root.val);
		traversal(list,root.left);
		traversal(list,root.right);

	}
非递归方法
  1. 根节点不为空,则将节点入栈
  2. 将栈内节点依次出栈,直至栈空
  3. 将栈顶节点出栈后加入List中
  4. 若出栈节点有右节点,将右节点入栈
  5. 若出栈节点有左节点,将左节点入栈

代码:

public ArrayList<Integer> preOrderTraversal2(TreeNode root){
		ArrayList<Integer> list = new ArrayList<>();
		Stack<TreeNode> stack = new Stack<>();
		if (root != null){
			stack.push(root);
		}
		while (!stack.empty()){
			TreeNode p = stack.pop();
			list.add(p.val);
			if (p.right != null){
				stack.push(p.right);
			}
			if (p.left != null){
				stack.push(p.left);
			}
		}
		return list;
	}
哈希

####### 题目
Given an array of integers, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
You may assume that each input would have exactly one solution.

Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2

解题思路
  1. 遍历数组,将数组值作为key,数据下标作为value,放到hashmap中
  2. 遍历数组时,计算当前值与target的差值,将差值作为key,冲哈希表中取出下标,若哈希表中有值,则返回这组下标

代码:

public int[] twoSum(int[] numbers, int target) {
		HashMap<Integer, Integer> map = new HashMap<>();
		int[] res = new int[2];
		for (int i = 0; i < numbers.length; i++) {
			int diff =  target - numbers[i];
			Integer value = map.get(diff);
			if (value != null && value != i){
				if (value > i){
					res[0] = i + 1;
					res[1] = value + 1;
				}else {
					res[0] = value + 1;
					res[1] = i + 1;
				}
				return res;
			}
			map.put(numbers[i], i);
		}
		return res;
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值