Ejemplo n.º 1
0
        public void Equal()
        {
            // arrange
            TreeNode n1 = new TreeNode(1);
            n1.left = new TreeNode(2);
            n1.right = new TreeNode(3);
            n1.right.left = new TreeNode(4);
            TreeNode n2 = new TreeNode(1);
            n2.left = new TreeNode(2);
            n2.right = new TreeNode(3);
            n2.right.left = new TreeNode(4);

            // assert
            Assert.IsTrue(s.IsSameTree(n1, n2));
        }
Ejemplo n.º 2
0
        public void nonEqual()
        {
            // arrange
            TreeNode n1 = new TreeNode(1);
            TreeNode n2 = new TreeNode(2);
            TreeNode n11 = new TreeNode(1);
            n11.right = new TreeNode(1);

            // assert
            Assert.IsFalse(s.IsSameTree(null, n1));

            Assert.IsFalse(s.IsSameTree(n2, n1));

            Assert.IsFalse(s.IsSameTree(n11, n1));
        }
Ejemplo n.º 3
0
        public bool IsSameTree(TreeNode p, TreeNode q)
        {
            if (p == q) {
                return true; // same reference
            }

            if (p == null ^ q == null) {
                return false; // only one null
            }

            if (q.val != p.val) {
                return false;
            }

            if (!this.IsSameTree(p.left, q.left)) { return false; }

            if (!this.IsSameTree(p.right, q.right)) { return false; }

            return true;
        }