Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static boolean isSameTree(TreeNode p, TreeNode q) {
- if (p == null && q == null) {
- return true;
- }
- if (p == null || q == null) {
- return false;
- }
- return p.val == q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
- }
- public static void dfs(TreeNode root, List<String> ans, String path) {
- if (root == null) {
- return;
- }
- // здесь мы точно не null, обновляем путь
- path += root.val;
- if (root.left == null && root.right == null) {
- ans.add(path);
- return; // мы в листе, дальше смысла спускаться нет
- }
- dfs(root.left, ans, path + "->");
- dfs(root.right, ans, path + "->");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement